Thursday, 18 May 2017

mocked funciton never caleld

I have this function in my implementation.js file:

  getForecast({ context, entities } : {context: Object, entities:Object}) {
    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 : Object) => {
      context.forecast = `It s currently ${responseJSON.current.temp_c} °C in ${location} And the weather conditions is: ${responseJSON.current.condition.text}`;
      delete (context.missingLocation : boolean);
      return context;
    });
    }
    delete (context.forecast : string);
    return context;
  }

And this is my unit test for this function:

  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();
    });
  })

When I run my test, i get this error : ExpectationError: Expected firstEntityValue once (never called)

I'm exporting my functions in implementation.js, I tried to replace the mock.verify, I tried some other syntaxes, I always get this error...



via ezdin gharbi

No comments:

Post a Comment