Monday 1 May 2017

Node. MongoDB - Sinon stub: How can I return a function which yields?

I am trying to test (with Sinon.JS Stubs) the following call with the Node.js MongoDB driver...

collection.find({mood: 'happy'}).toArray((err, result) => {

  // array result

  cb(null, result);
});

What is hanging me up here is the .toArray() chained function. The above call returns my expected results as an Array.


To demonstrate my struggle - and to contrast - I am able to stub the following call on a function call that is not chained as such...

collection.findOne({id: 'id'}, (err, result) => {

    // single result

    cb(null, result);
  });

stubb'd =>

findOneStub.yields(null, {username: 'craig'});

Is there a straightforward way to stub my .find call, which returns a function with .toArray on it to finally mock my results?



via scniro

No comments:

Post a Comment