更新
在我的('资源'测试中,我创建了一个新用户
/**
* root level hooks
*/
before((done) => {
const newUser = new User({
username: 'johndoe',
password: 'jdpwd',
email: 'johndoe@example.com',
mobileNumber: 123456789
});
newUser.save((err) => {
// console.log('error: ', err);
done();
});
});
我想在我的测试中重用用户ID:
describe('## Resource APIs', () => {
const user = User.findOne({ username: 'johndoe' });
console.log('got user: ', user.id);
let resource = {
name: 'RS123',
description: 'RS123Description',
owner: user._id, <= should be the id of the newUser
private: true
};
describe('# POST /api/v1/resources', () => {
it('should create a new resource', (done) => {
request(app)
.post('/api/v1/resources')
.send(resource)
.expect(httpStatus.OK)
.then((res) => {
expect(res.body.name).to.equal(resource.name);
expect(res.body.description).to.equal(resource.description);
resource = res.body;
done();
})
.catch(done);
});
});
...
});
但用户未定义..
我该怎么改变它? 感谢您的反馈
答案 0 :(得分:2)
如果使用this
在before块中声明变量,则可以在该fixture的所有测试中重复使用它:
describe('create draft', function() {
before(function() {
this.user = ...
});
it('....', function(){
this.user ....
});
});