簡體   English   中英

我如何鏈接承諾按順序運行Mocha集成測試?

[英]How can I chain promises sequentially running a mocha integration test?

我想測試兩個或多個Promise,例如集成測試,並且它們應按順序運行。 例子顯然是錯誤的,因為我只是從用戶(電子郵件)獲得用戶的屬性。

請注意,我在這里使用的是chai-promise,但是如果有更簡單的解決方案,則不必這樣做。

userStore返回一個諾言,如果它在其他測試中僅是一線問題,我可以解決它。

    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那么不能保證按順序運行對嗎?

鏈接諾言時,必須確保始終每個函數(包括.then()回調.then() return諾言。 在您的情況下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM