如何按顺序 运行 链接 promise 摩卡集成测试?
How can I chain promises sequentially running a mocha integration test?
我想像集成测试一样测试两个或多个 promise,它们应该按顺序 运行。示例显然是错误的,因为我作为用户仅从先前的测试(电子邮件)中获得 属性。
请注意,我在这里使用的是 chai-as-promised,但如果有更简单的解决方案,我不必使用。
userStore return是一个承诺,如果它在其他测试中只有一行没有问题,我可以解决它。
it.only('find a user and update him',()=>{
let user=userStore.find('testUser1');
return user.should.eventually.have.property('email','testUser1@email.com')
.then((user)=>{
user.location='austin,texas,usa';
userStore.save(user).should.eventually.have.property('location','austin,texas,usa');
});
});
如果我使用 return Promise.all
那么它不能保证按顺序 运行 对吗?
链接承诺时,您必须确保始终return
来自每个函数的承诺,包括.then()
回调。你的情况:
it.only('find a user and update him', () => {
let user = userStore.find('testUser1');
let savedUser = user.then((u) => {
u.location = 'austin,texas,usa';
return userStore.save(u);
// ^^^^^^
});
return Promise.all([
user.should.eventually.have.property('email', 'testUser1@email.com'),
savedUser.should.eventually.have.property('location', 'austin,texas,usa')
]);
});
带异步的 ES7:
it.only('find async a user and update him',async ()=>{
let user=await userService.find('testUser1');
expect(user).to.have.property('email','testUser1@email.com');
user.location = 'austin,texas,usa';
let savedUser=await userService.update(user);
expect(savedUser).to.have.property('location','austin,texas,usa');
});
我想像集成测试一样测试两个或多个 promise,它们应该按顺序 运行。示例显然是错误的,因为我作为用户仅从先前的测试(电子邮件)中获得 属性。
请注意,我在这里使用的是 chai-as-promised,但如果有更简单的解决方案,我不必使用。
userStore return是一个承诺,如果它在其他测试中只有一行没有问题,我可以解决它。
it.only('find a user and update him',()=>{
let user=userStore.find('testUser1');
return user.should.eventually.have.property('email','testUser1@email.com')
.then((user)=>{
user.location='austin,texas,usa';
userStore.save(user).should.eventually.have.property('location','austin,texas,usa');
});
});
如果我使用 return Promise.all
那么它不能保证按顺序 运行 对吗?
链接承诺时,您必须确保始终return
来自每个函数的承诺,包括.then()
回调。你的情况:
it.only('find a user and update him', () => {
let user = userStore.find('testUser1');
let savedUser = user.then((u) => {
u.location = 'austin,texas,usa';
return userStore.save(u);
// ^^^^^^
});
return Promise.all([
user.should.eventually.have.property('email', 'testUser1@email.com'),
savedUser.should.eventually.have.property('location', 'austin,texas,usa')
]);
});
带异步的 ES7:
it.only('find async a user and update him',async ()=>{
let user=await userService.find('testUser1');
expect(user).to.have.property('email','testUser1@email.com');
user.location = 'austin,texas,usa';
let savedUser=await userService.update(user);
expect(savedUser).to.have.property('location','austin,texas,usa');
});