2013-10-25 70 views
0

我正在尝试编写我的AngularJS应用程序的端到端测试,其中检查URL的某个参数何时发生。但是,$routeParams是一个空对象。 This guy也有同样的问题。我试图确定当?code=whatever位于URL中时,UI中会出现一些文本。我的应用程序的工作原理,以及这条路线做什么的预期:

$routeProvider.when '/', 
    resolve: 
    redirect: ($location, $routeParams, $http, $cookieStore) -> 
     console.log $routeParams 
     code = $routeParams.code 
     console.log 'code: ' + code 
     if code 
     // do something 
     else 
     // do something else 

当我通过噶测试跑步参观test-index.html?code=123,在浏览器控制台我看到:

Object {} 
code: undefined 

我希望有模拟出$routeParams在单元测试中,但我假设端到端测试会像真正的应用程序一样运行。为什么$routeParams在我的测试中完全清空,肯定存在URL参数?

编辑:这里是我的测试:

it 'fills in the input field when code is in URL', -> 
    browser().navigateTo '/base/test-index.html?code=123#/' 
    element('a.some-link').click() 
    expect(input('my_param.value').val()).toBe 'something that gets set when code in URL' 

回答

1

$route,$routeParams$location的大部分函数都在“#”之后的部分url上工作。因此,正如你的例子“/base/test-index.html?code=123#/”,角度只在“#”后面看到“/”,没有别的,这就是为什么你得到空$routeParams。另外,您不应在解析功能中使用$routeParams,而应使用$route.current.params

试图改变

browser().navigateTo '/base/test-index.html?code=123#/' 

browser().navigateTo '/base/test-index.html#/?code=123' 

而在你redirect

console.log $route.current.params 
0

这种感觉janky得要命,因为我改变实际,以适应测试,因为AngularJS没有填写$routeParams像它应该。感谢this generic JavaScript solution,我将QueryString添加到另一个JS文件,并将其包含在我的应用程序index.html和test-index.html中。然后,在我的routes.js中,我做了code = $routeParams.code || QueryString.code。因此,如果$routeParams是空白的,这似乎只在测试中发生,那么?code=blah参数值是通过良好的JavaScript解析获得的。