Javascript 使用Supertest运行Mocha测试时,Express routes从不执行

Javascript 使用Supertest运行Mocha测试时,Express routes从不执行,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,当运行应用程序并直接与邮递员联系时,以下是一条有效的路线: router.get("/profile", function(req, res) { var schema = schemas.filter(function(e) { return e.route === "profile"; }).pop(); if (schema !== undefined) { var schemaJson = require(schema.schemaFile); return r

当运行应用程序并直接与邮递员联系时,以下是一条有效的路线:

router.get("/profile", function(req, res) {
  var schema = schemas.filter(function(e) { return e.route === "profile"; }).pop();

  if (schema !== undefined) {
    var schemaJson = require(schema.schemaFile);
    return res.status(200).send(schemaJson);
  }
});
下面是一个测试:

var schemas = require("../../conf/schemas"),
  api_key = require("../../conf/api-keys").pop().key,
  app = require("../../app"),
  request = require("supertest");

    describe("CDM API Get Schema Operations", function() {
      it("Returns 404 for schema that do not exist", function (done) {
        request(app)
          .get("/schemas/profile")
          .end(function(err, res) {
            expect(res.statusCode).to.equal(500);
            //expect(res.body.thing).to.not.equal(null);
            done()
          });
      });
    });
在调试模式下运行时,将到达expect语句,并且始终带有错误。从未到达路由中的代码

我的app.js文件不会导出app对象(它是Express对象)


此错误通常包含不受支持的状态代码方法。

尝试此操作,更改路由和中间件:

router.get("/profile", function(req, res, next) { var schema = schemas.filter(function(e) { return e.route === "profile"; }).pop(); if (schema !== undefined) { var schemaJson = require(schema.schemaFile); return res.status(200).send(schemaJson); } else { var error = new Error('Not Found'); error.status = 404; next(error); } }); router.get(“/profile”,函数(req、res、next){ var schema=schemas.filter(函数(e){returne e.route==“profile”}).pop(); if(模式!==未定义){ var schemaJson=require(schema.schemaFile); 返回res.status(200).send(schemaJson); }否则{ var error=新错误(“未找到”); error.status=404; 下一步(错误); } }); 应用程序使用(功能(错误、请求、恢复、下一步){ 物件 .状态(错误状态| | 500) .发送({ message:err.message, 错误:{} }); });
这是WebStorm的一个问题。在重新安装和重新配置之后,它运行良好。除此之外,我没有其他解释。

我们可以看看
。/../app
模块。导出
?谢谢。你的问题似乎表明你的路线a)从未被击中,但b)在你的超级测试中预期被击中。如果预期被击中,那么你的路线也被击中。你确定supertest中的expect被击中了吗?如果有一个错误,那个错误会说什么?肯定的,它被击中了-我在使用Webstorm,它击中了那行的断点。但是,它在该路由文件中的任何时候都不会命中断点。错误为:HPE\u无效\u状态/无效状态HI,该错误似乎为。我不确定是什么原因导致了它,但肯定有一些不好的事情发生在那里,与supertest/express/node无关。所以问题是,如果我只是运行应用程序并向postman发出请求,它就可以正常工作。我从该端点得到了预期的响应,没有问题。没有其他活动部件可以干扰。现在两天的时间已经过去了,不要忘记接受自己的答案:-) router.get("/profile", function(req, res, next) { var schema = schemas.filter(function(e) { return e.route === "profile"; }).pop(); if (schema !== undefined) { var schemaJson = require(schema.schemaFile); return res.status(200).send(schemaJson); } else { var error = new Error('Not Found'); error.status = 404; next(error); } }); app.use(function(err, req, res, next) { res .status(err.status || 500) .send({ message: err.message, error: {} }); });