vendredi 29 septembre 2017

NodeJS Sinon Spy not evaluating as called although it is

I am trying to test a service that is calling another service which I am trying to spy on and evaluate it has been called. When stepping through with debugger I can see that it as been called, I can see the spy.called set to true with proper call arguments but when it returns it evaluates to false. I do not understand what I am doing wrong. Could anybody please help ?

service.js

const httpService = require('./requestService');

const processGreetingPostback = function postBackGreeting(event) {
    httpService.getClientName(senderId)
            .then((resp) => {
                let bodyObject = JSON.parse(resp);
                let reply = `Hi ${bodyObject.first_name}. ${greetingMessage}`;
                logger.debug('Replying: ', {senderId: senderId, message: reply});
                httpService.sendMessage(senderId, reply);
            })
            .catch((err) => {
                logger.error('Error in getting user name', {error: err});
                httpService.sendMessage(senderId, greetingMessage);
            }); 
};

service.spec.js

const expect = require('chai').expect;
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const stubHttpService = {};
const sinonLoggerSpy = {};
const postbackService = proxyquire('./postbackService',{'./requestService':stubHttpService,'../utils/logger':sinonLoggerSpy}); 
....
it('should call httpService without user name',()=>{
    event.sender = {id:1234};
    event.postback = {payload:"Greeting"};
    stubHttpService.getClientName = sinon.stub().rejects('no user');
    stubHttpService.sendMessage = sinon.spy();

    postbackService.processGreetingPostback(event);

    //neither chai nor sinon return true
    expect(stubHttpService.sendMessage.called).to.equal(true);
    sinon.assert.calledOnce(stubHttpService.sendMessage);
    expect(stubHttpService.sendMessage.getCall(0).args[0]).to.equal(1234);
});

Aucun commentaire:

Enregistrer un commentaire