0
我有,我已经写测试用例兴农间谍回调不被称为
var debug = require('debug')('kc-feed:source:fb');
var request = require('request');
var config = require('../../config').root;
exports.source = function fetchFeed (callback) {
var params = {
headers: {'content-type' : 'application/jsons'},
url: config.host + "v1/social/fb/sync_feed",
method: 'GET'
};
request(params, function(err, body, response) {
if(err) {
callback(err);
}
else {
var raw_data = JSON.parse(response);
callback(null, raw_data);
}
});
};
我的源文件,这是我的摩卡测试用例
var chai = require('chai'),
expect = chai.expect,
app = require('../app'),
rewire = require('rewire'),
fbfeed = rewire('../src/feed/source/fb_feed'),
supertest = require('supertest'),
sinon = require('sinon'),
nock = require('nock');
describe('test cases for fb feed file', function() {
var callback;
beforeEach(function(){
callback = sinon.spy();
});
after(function(){
nock.cleanAll();
});
it('callback should be called with the response', function(done) {
nock('http://localhost:3000/')
.get('/v1/social/fb/sync_feed')
.reply(200, {});
callback = sinon.spy();
fbfeed.source(callback);
sinon.assert.calledOnce(callback);
done();
});
it('callback should be called with the error', function(done) {
nock('http://localhost:3000/')
.get('/v1/social/fb/sync_feed')
.replyWithError(new Error('err'));
fbfeed.source(callback);
sinon.assert.calledOnce(callback);
done();
});
我的两个测试案例失败,因为它说回调被称为0次。但回调总是被调用。请帮忙。
它(,函数(完成){ \t \t诺克( '://本地主机3000/HTTP' '回调应该与错误称为' \t \t \t获得( '/ V1 /社会/ FB/sync_feed')\t \t \t \t .replyWithError(新错误( 'ERR')); \t \t fbfeed.source(功能(ERR,RAWDATA){ \t \t \t expect(err).to.equal('err'); \t \t \t done() \t \t}); \t}); 我得到错误Uncaught AssertionError:expected [Error:err] to equal'err' – krish
'err'是一个对象,而不是一个字符串。尝试'expect(err.message).to.equal('err')'。 – robertklep
谢谢!有用。我不明白错误的类型。现在它工作@robertklep – krish