Node.js 回调不是函数-Mocha/Mongoose
所以我试着查找这个bug,但似乎找不到适合我的bug的答案。我正在使用mocha和chai http测试一些API。我只是使用端点对应的RESTFUL方法(POST、GET、PUT)点击端点并检查响应(非常直接)。问题是,我的测试套件单独工作(如果我一次运行一个测试套件),但是当我使用gulp命令运行它们时。。。对于一些测试用例(如果您熟悉mocha的话,可以使用if钩子中的那些),我会得到“回调不是函数” 下面是我得到的错误:Node.js 回调不是函数-Mocha/Mongoose,node.js,mongoose,gulp,mocha.js,chai,Node.js,Mongoose,Gulp,Mocha.js,Chai,所以我试着查找这个bug,但似乎找不到适合我的bug的答案。我正在使用mocha和chai http测试一些API。我只是使用端点对应的RESTFUL方法(POST、GET、PUT)点击端点并检查响应(非常直接)。问题是,我的测试套件单独工作(如果我一次运行一个测试套件),但是当我使用gulp命令运行它们时。。。对于一些测试用例(如果您熟悉mocha的话,可以使用if钩子中的那些),我会得到“回调不是函数” 下面是我得到的错误: Uncaught TypeError: callback.app
Uncaught TypeError: callback.apply is not a function
at Immediate.<anonymous> (node_modules/mongoose/lib/model.js:3683:16)
at Immediate._onImmediate (node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)
&我有一个包含以下内容的吞咽文件:
// Set the environment variable for the test cases to point to production
gulp.task('set-testing-env', function() {
return process.env.NODE_ENV = 'production';
})
// gulp task for backend
gulp.task('test-backend', ['set-testing-env'], function() {
return gulp.src('test/backend/**/*.js', {read: false})
.pipe(mocha({
reporter: 'spec',
timeout: 60000
}))
.on('error', function(err) {
// console.log(err);
process.exit();
});
});
&最后是我的测试用例示例:
describe("testing GET" ....
before(function(done) {
... setting up stuff here and calling done ...
})
describe("get courses information and courses offered", function() {
it("gets a courses information", function(done) {
const endpoint = test.endpoint + "/courseInfo/" + test.course
chai.request(app)
.get(endpoint)
.end(function(err, res) {
expect(err, "Error hitting endpoint").to.be.null;
expect(res, "Expected data in json format").to.be.json;
expect(res, "Expected status to be 200").to.have.status(200);
expect(res.body, "Expected course info!").to.have.length(1);
expect(res.body[0],"Missing keys").to.have.all.keys(courseInfo);
done();
})
})
})
describe("testing POST" ...
before(function(done) {
... setting up and calling done ...
})
describe(...
it(...)
})
})
我不太清楚为什么会收到回调而不是函数错误:(
如果有任何帮助,我们将不胜感激。可能在您未包含在此处的部分代码中,您正在调用Mongoose方法,该方法将过多的对象作为参数传递,如下问题所示:
describe("testing GET" ....
before(function(done) {
... setting up stuff here and calling done ...
})
describe("get courses information and courses offered", function() {
it("gets a courses information", function(done) {
const endpoint = test.endpoint + "/courseInfo/" + test.course
chai.request(app)
.get(endpoint)
.end(function(err, res) {
expect(err, "Error hitting endpoint").to.be.null;
expect(res, "Expected data in json format").to.be.json;
expect(res, "Expected status to be 200").to.have.status(200);
expect(res.body, "Expected course info!").to.have.length(1);
expect(res.body[0],"Missing keys").to.have.all.keys(courseInfo);
done();
})
})
})
describe("testing POST" ...
before(function(done) {
... setting up and calling done ...
})
describe(...
it(...)
})
})