This is an issue related to asynchronous testing in Sails JS using Mocha.
I am writing controller test in Sails JS using supertest library. I want to check if publishCreate
method is being called on HTTP POST to our HeartbeatController#create
(See the code snippet). 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) 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