【问题标题】:expect is not defined in supertest-as-promised预期未在 supertest-as-promised 中定义
【发布时间】:2016-09-02 16:22:39
【问题描述】:

当我使用下面的代码时,显示的错误 expect is not defined(在 then 内部)

文档link

it("should return error", function () {
  return request(app).get("/verify")
    .expect(200)
    .then(function (res) {
      return expect(res.body.error[0].message).to.equal("NEW_CODE_REQUIRED");
    });
});

如何检查?

【问题讨论】:

    标签: node.js mongoose supertest-as-promised


    【解决方案1】:

    这有点疏忽了文档,更不用说包中不包含独立的 expect 函数。

    为此,您必须使用单独的包,例如 chai

    const expect = require('chai').expect;
    ...
    

    【讨论】:

    • 谢谢@robertklep,但我只能使用 supertest-as-promised 来解决它吗?
    【解决方案2】:

    我通过以下过程解决了它。添加了一个函数来检查预期的错误,如果得到意外的值则返回错误,并且这个函数从.expect()调用

    function checkErrorMessage(res) { // this function throw error if got unexpected result
       if(res.body.error[0].message === 'NEW_CODE_REQUIRED') {
         return false; // return false means no error (got expected result)
       } else {
         return true; // return true means return error (got unexpected result)
       }
    }
    
    it("should return error", function () {
      return request(app).get("/verify")
        .expect(200)
        .expect(checkErrorMessage);
    });
    

    【讨论】:

      猜你喜欢
      • 2017-12-06
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2018-06-01
      相关资源
      最近更新 更多