2017-08-27 79 views
1

我正在使用量角器柴爲我的最新的Angular 4應用程序承諾。我現在只有2個e2e測試。一個用戶在登錄,另一個登錄一個新用戶。如果註冊成功,我的註冊邏輯也將登錄新創建的用戶。柴承諾最終不會等待足夠長的角度測量e2e測試對角應用程序

下面的HTML代碼:

<ul class="navbar-nav my-2 my-lg-0"> 
    <li *ngIf="!isAuthenticated()" class="nav-item"> 
    <button id="loginLink" class="btn btn-link nav-link" style="line-height: inherit" type="button" role="button" (click)="openLoginModal()">Login</button> 
    </li> 
    <li *ngIf="!isAuthenticated() && signupAllowed()" class="nav-item"> 
    <button id="signupLink" class="btn btn-link nav-link" style="line-height: inherit" type="button" role="button" (click)="openSignupModal()">Sign up</button> 
    </li> 
    <li *ngIf="isAuthenticated()" class="nav-item"> 
    <a id="logoutLink" class="nav-link" role="button" (click)="logout()">Logout</a> 
    </li> 
</ul> 

而這裏的端對端測試代碼:

import { browser, by, element } from "protractor"; 

const expect = global[ 'chai' ].expect; 

describe('Auth',() => { 
    describe('login/logout',() => { 
    it('should login successfully with valid username and password tuple',() => { 
     // arrange 
     browser.get('/'); 
     expect(element(by.id('loginLink')).isPresent()).to.eventually.be.true; 
     element(by.id('loginLink')).click(); 

     // act 
     element(by.id('usernameInput')).sendKeys('jeep87c'); 
     element(by.id('passwordInput')).sendKeys('1111'); 
     element(by.id('loginBtn')).click(); 

     // assert 
     expect(element(by.id('logoutLink')).isPresent()).to.eventually.be.true; 
     element(by.id('logoutLink')).click(); 
     expect(element(by.id('loginLink')).isPresent().to.eventually.be.true; 
    }); 
    }); 

    describe('signup/logout',() => { 
    it('should succeeds with unused username and matching passwords and login',() => { 
     // arrange 
     browser.get('/'); 
     expect(element(by.id('signupLink')).isPresent()).to.eventually.be.true; 
     element(by.id('signupLink')).click(); 

     // act 
     element(by.id('usernameInput')).sendKeys('test'); 
     element(by.id('passwordInput')).sendKeys('1111'); 
     element(by.id('confirmPasswordInput')).sendKeys('1111'); 
     element(by.id('signupBtn')).click(); 

     // assert 
     expect(element(by.id('logoutLink')).isPresent()).to.eventually.be.true; 
     element(by.id('logoutLink')).click(); 
    }); 
    }); 
}); 

正如你可以看到,兩種測試都非常相似。但signup/logoutexpect(element(by.id('logoutLink')).isPresent()).to.eventually.be.true;上失敗,測試中也出現該錯誤,測試成功。

我錯過了什麼?感覺就像它根本不等待。但它適用於login/logout(意味着測試成功)。

登錄&對API的註冊請求都花費大約200毫秒來完成每個。需要注意的是,當用戶提交註冊表單時,它首先會觸發對api的註冊請求,如果成功,則會觸發登錄請求。所以它需要大約400ms才能完成。我猜測,其中,eventually將輪詢/等待。

編輯: 一些expect(...).to.eventually缺少上面的代碼,我修正了。 作爲一個僅供參考,這裏的斷言失敗:

Auth signup/logout should succeeds with unused username and matching passwords and login: 

    AssertionError: expected false to be true 

這真的感覺像承諾的決心之前的元素在DOM實際存在,但無法斷言。而不是再次輪詢它,直到它爲真或超時。

編輯#2: 剛試過完全相同的代碼,但在執行最後兩行之前等待5秒鐘,並順利通過......

setTimeout(function() { 
    expect(element(by.id('logoutLink')).isPresent()).to.eventually.be.true; 
    element(by.id('logoutLink')).click(); 
}, 5000); 

所以它肯定expect(element(by.id('logoutLink')).isPresent()).to.eventually.be.true;承諾解決快速爲假並沒有通過測試。我該如何解決?

任何幫助將不勝感激。

回答

0

我不知道這是造成問題,但是這一次,我從改變我的註冊代碼:

signup() { 
    this.auth.signup(signupData) 
     .subscribe({ 
        error: (err: any) => {}, 
        complete:() => { 
        this.auth.login(new LoginData(signupData.username, signupData.password)) 
         .subscribe({ 
             error: (err: any) => {}, 
             complete:() => { 
             this.activeModal.close(); 
             this.router.navigateByUrl('/'); 
             } 
            }); 
        } 
       }); 
} 

要:

signup() { 
    this.auth.signup(signupData) 
     .subscribe({ 
        next: (response) => this.auth.setToken(response.json().token), 
        error: (err: any) => {}, // TODO-jeep87c: Handle 403, 401 with proper UI alert and 500/404 in common handler (use username.setErrors) 
        complete:() => { 
        this.activeModal.close(); 
        this.router.navigateByUrl('/'); 
        } 
       }); 
} 

測試成功。我正在使用ng2-ui-auth作爲AngularJ2的原始Satellizer的Angular 2+端口。

它可能與我如何使用那些auth服務的方法返回的Obsersables有關。

如果有人能解釋,我會很樂意真正理解這裏發生了什麼。