我有這樣的方法before
在我的摩卡測試,其測試準備數據庫:摩卡沒有等到前完成
before(() => {
db.User.destroy({where: {}})
.then(db.User.create({id: 1, username: 'foo', password: 'secret', email: '[email protected]'}))
.then(db.Post.destroy({where: {}}))
.then(() => {
db.Post.create({id: 1, title: 'Foo', markdown: 'Bar', authorId: 1, published: true});
db.Post.create({id: 2, title: 'Bar', markdown: 'Foo', authorId: 1, published: false});
})
.catch(err => console.err(err));
});
我的第一個測試很簡單:
describe('[GET] /posts',() => {
it('expect to GET all posts', done => {
chai.request(server)
.get('/posts')
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body.length).to.be.equal(2);
done();
});
});
});
它不工作,我res.body.length
等於0,第二個測試:
describe('[GET] /posts?published=:bool',() => {
it('expect to GET all published posts', done => {
chai.request(server)
.get('/posts?published=true')
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body.length).to.be.equal(1);
done();
});
});
作品如預期結果在這裏。最後,當我檢查我的數據庫時,兩個帖子在這裏。當我使用郵遞員地址/posts
也按預期工作...