最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

摩卡:未捕获的AssertionError:预计200至404将

运维笔记admin11浏览0评论

摩卡:未捕获的AssertionError:预计200至404将

摩卡:未捕获的AssertionError:预计200至404将

我想测试随机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
回答如下:

我相信你需要在你的服务器/应用/ 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";
});

发布评论

评论列表(0)

  1. 暂无评论