2014-11-07 19 views
0

我在註冊AngularJs應用程序後測試重定向。 點擊註冊按鈕後,我調用一個函數來檢查url是否與targetRegex匹配。 第一個代碼塊使用茉莉花expect但我得到的錯誤:timeout: timed out after 30000 msec waiting for spec to complete使用regex.test()而不是Jasmine測試重定向期望

return browser.wait(function() { 
    return browser.driver.getCurrentUrl().then(function(url) { 
     return expect(url).toContain(targetRegex); 
    }); 
    }); 

同時下面的代碼似乎運作良好:

return browser.wait(function() { 
    return browser.driver.getCurrentUrl().then(function(url) { 
     return targetRegex.test(url); // look for a match of the regex /profile/ in the 'url' 
    }); 
    }); 

是任何人能解釋我爲什麼嗎?

+0

第一個失敗時的實際URL是什麼? – 2014-11-07 03:03:17

+0

from'console.log(url)'是'http:// localhost/profile/200490' – 2014-11-10 00:45:21

回答

0

Jasmine docs'toContain'匹配器用於查找陣列中的物品

對於通過正則表達式進行匹配,我認爲您想使用toMatch來代替,如expect(url).toMatch(targetRegex);

但是,我不確定是否要在return之後使用toMatch方法。它不返回簡單的布爾結果。如果你看看source code for that method,你會發現它返回一個帶有compare方法的對象。從匹配器子目錄中的其他方法的源代碼來看,顯然這是Jasmine庫內部使用的標準模式。

所以,底線,你可能會更好地堅持return targetRegex.test(url);方法,因爲它返回一個布爾值。

+0

ok thx我明白了!無論如何,在測試中使用普通的Js是否很好?我不應該在「我正在檢查一個條件」時使用茉莉花的期望嗎? – 2014-11-18 18:09:26