3 回答

TA貢獻1807條經驗 獲得超9個贊
遵循要進行的更改以使 Mocha 測試正常工作
在 中index.js,您應該添加此行module.exports = app;以便服務器啟動。
const express = require("express");
const app = express();
const homeController = require("./routes/home");
app.use("/home", homeController);
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log("Server started at port", port);
});
module.exports = app;
在 中test/routes/home.js,您應該添加.get("/home")和res.text.should.equal("Hello World!"); 來通過/home端點的Mocha 測試
const chai = require("chai");
const chaiHTTP = require("chai-http");
const mainEntryPoint = require("../../index");
chai.should();
chai.use(chaiHTTP);
describe("Entry point", () => {
context("GET /", () => {
it("should get greeting back", (done) => {
chai.request(mainEntryPoint)
.get("/home")
.end((err, res) => {
if (err) throw err;
res.text.should.equal("Hello World!");
done();
});
});
});
});

TA貢獻1794條經驗 獲得超8個贊
請嘗試:
....
.get("/") // This should be a relative path from current route
.end((err, response) =>{
if(err) return done(err)
response.should.have.status(200)
response.body.should.be('string')
done()
});
如果沒有看到路線代碼,我不能百分百確定。但是,TypeError: cannot do whatever with undefined當您訪問未定義的對象屬性或方法時,就會發生這種情況。我建議打印回復以獲得一些見解。另外,請分享您的路線邏輯。如果沒有它,就很難確定錯誤的根源

TA貢獻1817條經驗 獲得超14個贊
也許你應該嘗試:
const app = require("./app.js")
chai.request(app).get("/api/chow/users")
chai.request(app).get("/api/chow/users")
或者
chai.request(home).get("/")
chai.request(user).get("/")
添加回答
舉報