未捕获的TypeError:无法读取null的属性“status”

时间:2018-04-09 00:13:59

标签: node.js mocha chai supertest

我正在尝试编写单元测试,如果没有“description”属性,则不允许发布“问题”。但是,每当我尝试运行测试时,我都会收到Uncaught TypeError: Cannot read property 'status' of null

it('it should not POST a question with no description field' , (done) => {
    let data = {
        'isMeeting':true,
        'expId':'1234'
    }
    request(app)
        .post('http://localhost:3000/api/questions', data)
        .send(data)
        .end((err, res) => {
            res.status.should.be.equal(200);
            res.body.should.be.a('object');
            res.body.should.have.property('errors');
            res.body.errors.should.have.property('description');
            res.body.errors.description.should.have.property('kind').eql('required');
            done();
        }); 
})

这是完整的单元测试代码尝试:

const chai = require('chai');
const request = require('supertest');
const assert = require('assert');
const app = require('../server/server');
var should = chai.should();

let Questions = require('../common/models/questions');

describe ('send ques', function(){
    it('test case for post questions', function(){
        let data={'description' : 'I need help in ecommerce',
            'isMeeting':true,
            'expID':'1234'
        }
        request (app)
            .post('http://localhost:3000/api/questions', data)
            .set('accept', 'application/json')
            .expect('Content-Type', /json/)
            .expect(200);
    });
    it('it should not POST a question with no description field' , (done) => {
        let data = {
            'isMeeting':true,
            'expId':'1234'
        }
        request(app)
            .post('http://localhost:3000/api/questions', data)
            .send(data)
            .end((err, res) => {
                res.status.should.be.equal(200);
                res.body.should.be.a('object');
                res.body.should.have.property('errors');
                res.body.errors.should.have.property('description');
                res.body.errors.description.should.have.property('kind')
                    .eql('required');
                done();
            }); 
    }) 
})

第一次测试没有问题

0 个答案:

没有答案
相关问题