亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Mocha 單元測試:未捕獲類型錯誤:無法讀取未定義的屬性“應用”

Mocha 單元測試:未捕獲類型錯誤:無法讀取未定義的屬性“應用”

白衣染霜花 2024-01-18 10:46:20
我想為 Restful Endpoint 編寫單元測試,但每當我運行 npm test 時。我收到錯誤 Uncaught TypeError: Cannot read property 'apply' of undefined 。let chai = require('chai')let chaiHttp = require('chai-http')let home = require('../routes/home')let user = require('../routes/users')//Assertion Stylechai.should()chai.use(chaiHttp)describe('chow Api', () => {  //  users    describe("GET /api/chow/home", () => {        it('it should render Welcome to Homepage', (done) =>{            chai.request(home)            .get("/api/chow/home")            .end((err, response) =>{                response.should.have.status(200)                response.body.should.be('string')                done()            })        })    })    describe('GET /api/chow/users', () => {        it('it should GET all the users', (done) => {            chai.request(user)            .get("/api/chow/users")            .end((err, response) =>{                response.should.have.status(200)                response.should.be.a('array')                done()            })        })    })})
查看完整描述

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();

                });

        });

    });

});


查看完整回答
反對 回復 2024-01-18
?
幕布斯7119047

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當您訪問未定義的對象屬性或方法時,就會發生這種情況。我建議打印回復以獲得一些見解。另外,請分享您的路線邏輯。如果沒有它,就很難確定錯誤的根源


查看完整回答
反對 回復 2024-01-18
?
大話西游666

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("/")


查看完整回答
反對 回復 2024-01-18
  • 3 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號