我正在编写一个博客平台,在网络服务器上测试时一切都运行良好。但是,我正在尝试使用 Mocha 和 Should.js 编写单元测试,但我遇到了不应该出现错误的错误。例如,在下面的代码中,每当我尝试向回调函数(第三个参数)实际添加某些内容时,例如调用 done()
或声明类似 fakeReq.entries.should.exist 的内容
,我收到一百万个错误:
describe("#load()", function(done){
entries.load(fakeReq,fakeRes,function(){},"my-first-post")
})
该函数如下所示:
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()
})
}
然而,就这样留下来看起来好像什么都没有经过测试。从我的命令行(请注意,上述代码行位于 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
有没有人有丰富的 Mocha 经验可以帮助我?我认为我不能简单地使用 before()
或 beforeEach()
语句访问 Mongo,因为测试的一部分是确保我的代码正确访问数据库。
最佳答案
您需要调用it
函数进行实际测试。 describe
它来描述一组相关的测试,然后在 describe
回调中调用 it
就是实际的测试。
describe("my module", function () {
it("should require OK", function () {
require("../my-module").should.exist
});
});
关于javascript - Mocha 中无法修复的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18130362/