How to test if a method is being called in the controller?

53 views
Skip to first unread message

Yedhu Krishnan

unread,
Nov 24, 2014, 12:25:45 AM11/24/14
to mocha-d...@googlegroups.com

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

floehopper

unread,
Nov 24, 2014, 3:53:26 PM11/24/14
to mocha-d...@googlegroups.com
This is the mailing list for the Ruby mocking library, Mocha.

I suspect you want the JS testing library - http://mochajs.org/

Regards, James.
Reply all
Reply to author
Forward
0 new messages