Mocha with Promises II.
I'm testing a collaborating unit, that passed the result of notesList module to buildPage module.
notesList returns a promise. Here's how I stub it (I'm testing the router module):
before(function () {
subject = SandboxedModule.require('../../router', {
singleOnly: true,
requires: {
'./buildPage': buildPage = sinon.stub(),
'./notesList': notesList = sinon.stub()
}
});
});
//...
before("notesList returns an html fragment in a promise", function () {
notesList.withArgs(sinon.match.string).returns(
new Promise(function (resolve) {
resolve("<pants>fragment</pants>");
})
);
});
Then to test if my router module passes correctly the results:
context("responds to '', '/','/oracle','/oracle/','/oracle/...' routes", function(){
before("called with ''", function () {
subject({url: ""});
});
before("called with '/'", function () {
subject({url: "/"});
});
// ...
it("calls buildPage with the result of notesList", function(done){
expect(buildPage.calledWith("<pants>fragment</pants>")).to.be.ok;
done();
});
});
At line #9 I added the done
parameter to make it wait for the Promise to return. In my router module I make the test pass by:
notesList("pants").then(buildPage);