从未调用过模拟函数

时间:2017-05-18 16:27:07

标签: 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=${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 once (never called)

imp:const imp = require('../build/implementation.js');

我在implementation.js中导出我的函数,我试图替换mock.verify()定位,我尝试了一些其他语法,我总是得到这个错误......

0 个答案:

没有答案