【发布时间】:2017-08-02 15:05:48
【问题描述】:
我还在学习node、js、sinon、proxyquire等。
我有一个使用 google-geocode 模块 (https://github.com/bigmountainideas/google-geocoder) 的模块,我正在努力编写一个测试来存根它。
这一切都归结为我认为你是如何设置它的。在 time.js 中,我按照 google-geocoder 文档执行以下操作:
var geocoder = require('google-geocoder');
...
module.exports = function(args, callback) {
var geo = geocoder({ key: some-thing });
geo.find('new york', function(err, response) { ... });
}
我正在尝试如下测试,但出现错误:
TypeError: geo.find is not a function
at run (cmdsUser/time.js:x:x)
at Context.<anonymous> (tests/cmdsUser/time-test.js:x:x)
time-test.js:
var time;
var findStub;
before(function () {
findStub = sinon.stub()
time = proxyquire('./../../cmdsUser/time',{ 'google-geocoder': { find: findStub } } );
});
describe('Demo test', function() {
it('Test 1', function(done){
findStub.withArgs('gobbledegook').yields(null, { this-is: { an-example: 'invalid' } });
time(['gobbledegook'], function(err, response) {
expect(response).to.equals('No result for gobbledegook');
done();
});
});
});
我有点困惑。非常感谢。
【问题讨论】:
标签: node.js unit-testing sinon proxyquire