robzolkos
robzolkos

Reputation: 2276

Testing node server with mocha and nested callbacks

I'm learning node and mocha and have the test below (production code increments a field in the database).

To see if its working, I'm outputting the value of this field before and after a call to /events (see the two console.log statements).

At the moment, the value output is 1 in both cases. Outside of the test the code is working fine and the field is incremented.

I suspect its something to do with the callbacks in my test code (as I'm still learning about how to correctly write and test node code this is almost certainly the case).

Hopefully someone can spot the problem. The production code this is testing is here (gist).

it('"processed" event increases processed count', function(done) {
  Edm.find({campaignguid: '1234'}).success(function(edm) {
    edmProcessedCount = edm.processed;
    console.log("pre: " + edmProcessedCount);  // result is 1

    request.post('http://localhost:3001/events?campaignguid=1234&event=processed', function(err, res, body) {
      Edm.find({campaignguid: '1234'}).success(function(edmm) {
        console.log("post : " + edmm.processed);  // result is 1 (should be 2)
        done();
      });
    })

  });
});

Upvotes: 0

Views: 352

Answers (1)

robertklep
robertklep

Reputation: 203359

Your query in the Mocha test case is missing a where clause:

Edm.find({ where : { campaignguid: '1234' }})
           ^^^^^

Upvotes: 1

Related Questions