This is an issue related to asynchronous testing in Sails JS using Mocha.
I am writing controller test in Sails JS using supertest <https://github.com/tj/supertest> library. I want to check if publishCreate method is being called on HTTP POST to our HeartbeatController#create (See the code snippet <https://github.com/multunus/one-mdm/blob/master/api/controllers/HeartbeatController.js#L23>). For that, I want to stub the method and expecting it to be invoked in the end() as follows: request(sails.hooks.http.app) .post('/heartbeat/create') .send({ device: 1 }) .end(function(err, res) { setTimeout(function() { expect(publishCreateStub.callCount).to.equal(1); publishCreateStub.restore(); done(); }, 1000); }); When I run this, the expectation fails because the method is not called at the time of assertion. But when I put the expectation in a setTimeout (See the code snippet <https://github.com/multunus/one-mdm/blob/master/test/controllers/HeartbeatController.test.js#L39>) as follows, it works: request(sails.hooks.http.app) .post('/heartbeat/create') .send({ device: 1 }) .end(function(err, res) { setTimeout(function() { expect(publishCreateStub.callCount).to.equal(1); publishCreateStub.restore(); done(); }, 1000); }); Is there any way to make the test pass without a setTimeout? You can also help us to resolve the issue by sending pull requests: https://github.com/multunus/one-mdm/issues/1 -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/69370eca-6788-432c-8098-0f40886de219%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
