摩卡:Uncaught AssertionError:预计200为404

时间:2015-09-22 11:04:31

标签: node.js testing mocha assertions

我正在尝试测试随机urr。哪个不退出。所以响应代码应该是404,但它返回200,这使得测试用例失败,直到这里很好但它正在给Uncaught AssertionError: expected 200 to be 404如何处理这个未被捕获的断言错误

it("Should return 404 when you put random url after /api/random", function(done){    

            server
            .post("/api/random")
            .expect(404)
            .end(function(err, res){
              res.status.should.equal(404);
              done();
            })        
  });

未捕获的AssertionError 如何处理此问题..

 Uncaught AssertionError: expected 200 to be 404
      + expected - actual

      -200
      +404

      at Assertion.fail (node_modules/should/lib/assertion.js:180:17)
      at Assertion.prop.value (node_modules/should/lib/assertion.js:65:17)
      at Test.<anonymous> (test.js:48:33)
      at Test.assert (node_modules/supertest/lib/test.js:156:6)
      at assert (node_modules/supertest/lib/test.js:127:12)
      at node_modules/supertest/lib/test.js:124:5
      at Test.Request.callback (node_modules/supertest/node_modules/superagent/lib/node/index.js:785:12)
      at IncomingMessage.<anonymous> (node_modules/supertest/node_modules/superagent/lib/node/index.js:990:12)
      at _stream_readable.js:920:16

1 个答案:

答案 0 :(得分:0)

我相信您在server / app / main.js上需要一个错误处理程序,内容如下:

// catch 404 and forward to the error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.use(function(err, req, res, next) {

  // responds to client or test with error
  res.status(err.status || 500).json({
 message: "this is not the web page you're looking for. - obi-wan codenobi";
});