Node.js 从未调用模拟函数
我在implementation.js文件中有这个函数:Node.js 从未调用模拟函数,node.js,unit-testing,sinon,Node.js,Unit Testing,Sinon,我在implementation.js文件中有这个函数: getForecast({ context, entities } ) { const location = firstEntityValue(entities, 'location'); if (location) { /* global fetch */ return fetch(`https://api.apixu.com/v1/forecast.json?key=someKey&q
getForecast({ context, entities } ) {
const location = firstEntityValue(entities, 'location');
if (location) {
/* global fetch */
return fetch(`https://api.apixu.com/v1/forecast.json?key=someKey&q=${location}`)
.then(response => response.json())
.then((responseJSON) => {
context.forecast = `whatever`;
delete (context.missingLocation : boolean);
return context;
});
}
delete (context.forecast);
return context;
}
这是我对这个函数的单元测试:
it('should return the expected context when location is valid', () => {
const firstEntityValueMock = sinon.mock(imp);
console.log(firstEntityValueMock);
firstEntityValueMock.expects('firstEntityValue')
.once()
.withArgs(entities, 'location')
.returns('Paris');
const scope = nock('https://api.apixu.com')
.get(/.*/)
.reply(200, fetchResult);
const currentResult = `whatever`;
return imp.actions.getForecast({ context, entities })
.then((resultContext) => {
const res1 = _.lowerCase(resultContext.forecast);
const res2 = _.lowerCase(currentResult);
expect(res1).to.eql(res2);
expect(resultContext.missingLocation).to.be.undefined;
firstEntityValueMock.verify();
});
});
当我运行测试时,我得到以下错误:ExpectationError:Expected firstEntityValue一次(从未调用)
imp:const imp=require('../build/implementation.js')代码>
我正在implementation.js中导出我的函数,我试图替换mock.verify()定位,我尝试了一些其他语法,我总是遇到这个错误…在implementation.js
中定义的firstEntityValue
在哪里