2013-10-25 67 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解析獲得的。