茉莉'在测试我的REST API时,s afterEach似乎在beforeEach之前运行
Jasmine's afterEach seems to be running before the beforeEach in testing my REST API
代码:
describe('GET /:id', function() {
var post;
beforeEach(function() {
var options = {
uri: base_url,
method: 'POST',
json: {
title: 'one'
}
};
request(options, function(err, response, body) {
console.log('body: ', body);
if (err) console.log('ERROR IN THE SET UP.');
else { post = body; }
});
});
afterEach(function() {
console.log('in afterEach');
console.log('post: ', post);
request.del(base_url+'/'+post._id, function(err, response, body) {
if (err) console.log('ERROR IN THE TEAR DOWN.');
else { post = null; }
});
});
it('returns a status code of 200', function(done) {
expect(true).toBe(true);
done();
// request.get(base_url+'/'+post._id, function(err, response, body) {
// expect(response.statusCode).toBe(200);
// done();
// });
});
it('returns the right post', function(done) {
expect(true).toBe(true);
done();
// request.get(base_url+'/'+post._id, function(err, response, body) {
// expect(JSON.parse(body)).toEqual(post);
// done();
// });
});
});
输出:
~/code/study-buddies $ jasmine-node server
in afterEach
post: undefined
Fin afterEach
post: undefined
F
Failures:
1) Posts GET /:id returns a status code of 200
Message:
TypeError: Cannot read property '_id' of undefined
Stacktrace:
TypeError: Cannot read property '_id' of undefined
at null.<anonymous> (/Users/azerner/code/study-buddies/server/posts/post.spec.js:40:36)
at Timer.listOnTimeout (timers.js:119:15)
2) Posts GET /:id returns the right post
Message:
TypeError: Cannot read property '_id' of undefined
Stacktrace:
TypeError: Cannot read property '_id' of undefined
at null.<anonymous> (/Users/azerner/code/study-buddies/server/posts/post.spec.js:40:36)
Finished in 0.02 seconds
2 tests, 4 assertions, 2 failures, 0 skipped
body: { title: 'one', _id: '55b1aaca81a6107523693a00' }
body: { title: 'one', _id: '55b1aaca81a6107523693a01' }
~/code/study-buddies $
CCD_ 1记录在CCD_ 2之前。这让我认为afterEach
在beforeEach
之前运行。这是怎么回事?
使用done
回调同步运行beforeEach:
beforeEach(function(done) {
var options = {
uri: base_url,
method: 'POST',
json: {
title: 'one'
}
};
request(options, function(err, response, body) {
console.log('body: ', body);
if (err) console.log('ERROR IN THE SET UP.');
else { post = body; }
done();
});
});
相关文章:
- Mocha 的 beforeEach() 和 done() 函数不起作用
- 在jasmine描述函数中,自动向beforeEach和afterEach添加代码
- beforeEach 不会在模块化 QUnit/Sinon 测试中使用 CoffeeScript 中的 Require.
- 如何为 Jasmine/Angular 创建一个帮助程序来组合多个 beforeEach
- 如果“beforeEach”中的代码是异步的,如何在 Jasmine 中测试方法
- `beforeEach` Behavior with Nested Tests in Intern
- 如何在Jasmine JS中重用beforeEach/afterEach
- Jasmine 2.0 async beforeEach不等待async完成
- 使用Jasmine进行单元测试:测试没有看到beforeEach()中的代码's spyOn()
- 你能在beforeEach中有多个spyObjs吗?
- 如何在vue-router's beforeEach中使用vue-router's的动作?
- 全局BeforeEach,用于用Mocha和Angular模拟HTTP请求
- js测试bail(false)对beforeEach不起作用
- 在beforeEach期间将vuex模块状态传递给vue-router
- 在Mocha的afterEach钩子中检测测试失败
- 茉莉'在测试我的REST API时,s afterEach似乎在beforeEach之前运行
- (窗口.beforeEach || window.setup)不是一个函数
- JSHint警告Mocha 'beforeEach'即使使用"mocha": tru
- 摩卡测试没有按正确的顺序运行,使用before/beforeEach
- 在mocha.js和chai.js中基本但正确地使用beforeEach()或afterEach()