【问题标题】:Callback not a function - Mocha/Mongoose回调不是函数 - Mocha/Mongoose
【发布时间】:2017-08-28 02:18:53
【问题描述】:

所以我尝试查找这个错误,但我似乎无法找到适合我的错误的答案。我正在使用 mocha 和 chai-http 来测试一些 API。我只是使用它们相应的 RESTFUL 方法(POST、GET、PUT)访问端点并检查响应(非常直接)。问题是,我的测试套件单独工作(如果我一次运行一个),但是当我使用我的 gulp 命令运行它们时......我得到一些测试用例的“回调不是函数”(如果你熟悉 mocha,就在 if 钩子中)

这是我得到的错误:

 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)

这是我的测试用例所在目录的结构:

test
   backend
       assignments_tests.js
       application_tests.js
       courses_test.js

& 我有一个包含以下内容的 gulp 文件:

// 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(...)
     })
  })

我不太清楚为什么我收到的是回调而不是函数错误。 :( 任何帮助将不胜感激!

【问题讨论】:

    标签: node.js mongoose gulp mocha.js chai


    【解决方案1】:

    可能在您未在此处包含的代码的某些部分中,您正在调用 Mongoose 方法,将太多对象作为参数传递,就像在这个问题中一样:

    其中一个对象被解释为函数,但不能被这样调用。

    这是一个常见问题,但您没有包含任何运行 Mongoose 方法的代码,因此很难判断这是否是这里的问题。

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 2016-08-09
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多