Question

I am writing a blogging platform and everything works wonderfully when tested on a web server. However, I am trying to write unit tests using Mocha and Should.js and I am coming across errors where there shouldn't be errors. For example, in the following code, whenever I try to actually add something to the callback function (3rd parameter), like calling done() or stating something like fakeReq.entries.should.exist, I get a million errors:

describe("#load()", function(done){
    entries.load(fakeReq,fakeRes,function(){},"my-first-post")
})

and here is what the function looks like:

exports.load = function(req,res,next,slug){
    var User = mongoose.model('User')
    Entry.load(req.param('year'), req.param('month'), slug, function (err, article) {
        if (err) return next(err)
        req.article = article
        next()
    })
}

However, leaving it like this makes it seem like nothing ever gets tested. From my command line (note that the above lines of code are in Entries):

Entries
  #show()
    ✓ should render something 

EntrySchema
  #from_fake
    ◦ should have a title: TEST
    ✓ should have a title 
    ◦ should have a slug: test
    ✓ should have a slug 

Does anyone have a lot of experience with Mocha that can help me out? I don't think I can simply access Mongo with a before() or beforeEach() statement because part of the test is making sure my code accesses the database correctly.

Was it helpful?

Solution

You need to call the it function for your actual tests. describe it to describe a group of related tests and then calls to it within the describe callback are the actual tests.

describe("my module", function () {
  it("should require OK", function () {
      require("../my-module").should.exist
  });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top