简体   繁体   English

NodeJS Chai Mocha 响应未定义

[英]NodeJS Chai Mocha response undefined

I create my test code using typescript and run my test using mocha (mocha --timeout 10000)我使用 typescript 创建我的测试代码并使用 mocha (mocha --timeout 10000) 运行我的测试

The following is my code:以下是我的代码:

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

The error that I got:我得到的错误: 在此处输入图像描述

Clearly, res.status exist.很明显,res.status 是存在的。

Why res.should.have.status produce the undefined error?为什么 res.should.have.status 会产生未定义的错误?

Actually, I tried other things such as should.have.property nad I got undefined as well.实际上,我尝试了其他东西,比如 should.have.property nad 我也没有定义。

Thanks in advance提前致谢

From the assertion styles#should , Chai extends each object with a should property after calling chai.should() .断言 styles#should中,Chai 在调用chai.should()之后用一个should属性扩展了每个 object。

Which means chai will add should property to Object.prototype after calling chai.should() .这意味着 chai 将在调用chai.should()后将should属性添加到Object.prototype

The should interface extends Object.prototype to provide a single getter as the starting point for your language assertions. should接口扩展Object.prototype以提供单个 getter 作为语言断言的起点。

Eg例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM