2014-11-07 62 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