我有一条路线,我正在那条路线上进行摩卡测试。测试失败并显示错误消息 Uncaught AssertionError:预期未定义等于201'
我已经通过了测试用例,一切似乎都很好。任何有想法可能有问题的人
describe('AppComponent', () => {
const numberService:NumberService = new NumberService();
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AppComponent ],
imports: [
TranslateModule.forRoot(),
],
providers: [
{ provide: NumberService, useValue: numberService},
]
})
.compileComponents();
}));
it('should display ......', () => {
numberService.next('abc');
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
app.ngOnInit();
expect(app.number).toBe('abc');
});
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../src/app';
import vehicles from '../src/db/carDb';
const { expect } = chai;
chai.use(chaiHttp);
describe('Car', () => {
it('should create new car in app', (done) => {
const car = {
state: 'used',
status: 'available',
price: 2000000,
manufacturer: 'toyota',
model: 'camry',
bodyType: 'car',
};
chai.request(app)
.post('/api/v1/car')
.send(car)
.end((err, res) => {
const { body } = res;
if (err) done(err);
expect(res).to.be.an('object');
expect(body.status).to.equal(201);
expect(res.body.message).to.be.equal('Vehicle created successfully');
done();
});
});
});
我希望测试通过,但是应用返回未定义
答案 0 :(得分:0)
expect(body.status).to.equal(201);
您的错误表明body.status
是undefined
。
如果您查看chai-http
的{{3}}并查找status
,您会发现它们使用了专门设计的语法:
expect(res).to.have.status(201);
但是,您可能(没有对其进行测试)以documentation http.ServerResponse
实例的形式到达状态代码,该实例不会出现在主体中,而是:
expect(res.statusCode).to.equal(201);