我使用打字稿創建我的測驗代碼并使用 mocha (mocha --timeout 10000) 運行我的測驗
以下是我的代碼:
import chai from 'chai';
import chai_http from 'chai-http';
chai.use(chai_http);
describe('upload', () => {
beforeEach((done) => {
done();
});
describe('/POST upload', () => {
it('it should not POST a book without pages field', (done) => {
let book = {
title: "Test"
}
chai.request('http://192.55.55.19:3000')
.post('/upload')
.set('Content-Type', 'application/json')
.send(book)
.end((err, res) => {
console.log(`\ntesting3: ${JSON.stringify(res.status)}`);
res.should.have.status(200);
done();
});
});
});
});
我得到的錯誤:

顯然, res.status 存在。
為什么 res.should.have.status 會產生未定義的錯誤?
實際上,我嘗試了其他東西,例如 should.have.property nad 我也沒有定義。
提前致謝
uj5u.com熱心網友回復:
從斷言 styles#shouldshould中,Chai在呼叫 后擴展了每個物件的屬性chai.should()。
這意味著 chai 將在呼叫后添加should屬性。Object.prototypechai.should()
該
should介面擴展Object.prototype為提供一個 getter 作為語言斷言的起點。
例如
const chai = require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
chai.should();
describe('71144510', () => {
it('should pass', () => {
const res = { status: 200 };
res.should.have.status(200);
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427122.html
標籤:节点.js 单元测试 mocha.js 柴 柴http
