我如何链接承诺连续运行摩卡集成testing?

我想testing两个或更多的承诺,如集成testing,他们应该顺序运行。 示例显然是错误的,因为我仅从以前的testing(电子邮件)获取用户属性。

请注意,我在这里使用了chai-as-promise,但是如果有更简单的解决scheme,我不需要。

userStore返回一个承诺,我可以解决它,如果它只有一个class轮在其他testing没有问题。

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()callback。 在你的情况下:

 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与asynchronous:

 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'); });