user1493376 user1493376 - 4 months ago 12
Node.js Question

Stub doesn't return a value

I'm using Mocha as test runner, Chai for assertion and Sinon.
I'm having trouble using sinon, I have the following function that I want to test in PrestigeQuoteService.js file

find: function (criteria) {
return PrestigeQuote.find(criteria)
.populate('client')
.populate('vehicle')
.populate('quoteLogs');
},


and here is my test case

describe('find()', function () {
describe('prestige quote found', function () {
before(function () {
sandbox = sinon.sandbox.create();
var mockChain = {
populate: function () {
return this;
}
};
sandbox
.stub(PrestigeQuote, 'find').returns(mockChain);
});

it('should return a particular quote', function (done) {
PrestigeQuoteService.find({id: 1}, function (err, result) {
result.should.exist;
done();
});
});

after(function () {
sandbox.restore();
});
});
});


yet I get this error, even thought I have done() and should return value by default.

Error: timeout of 10000ms exceeded. Ensure the done() callback is being called in this test.

Answer

I solved it by adding a return in mockChain

 describe('prestige quote found', function () {
      before(function () {
        sandbox = sinon.sandbox.create();
        var err = null;
        var mockChain = {
          populate: function () {
            return this;
          },
          return:function () {
            return {};
          }
        };
        sandbox
          .stub(PrestigeQuote, 'find').returns(mockChain);
      });

      it('should return a particular  prestige quote', function (done) {
       PrestigeQuoteService.find({id: 1}, function (result) {
          result.should.exist;
        });
        done();
      });

      after(function () {
        sandbox.restore();
      });
    });
Comments