gpt4 book ai didi

AngularJS e2e 测试 - $routeParams 始终为空

转载 作者:行者123 更新时间:2023-11-28 21:31:34 24 4
gpt4 key购买 nike

我正在尝试编写我的 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

当我通过 Karma 测试运行器访问 test-index.html?code=123 时,在浏览器控制台中我看到:

Object {}
code: undefined

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

编辑:这是我的测试:

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'

最佳答案

$route$routeParams$location 的大部分函数都在“#”之后的url 部分工作。因此,在您的示例“/base/test-index.html?code=123#/”中,angular 仅在“#”之后看到“/”,没有别的,这就是为什么您的 $routeParams。此外,您不应在 resolve 函数中使用 $routeParams,而应使用 $route.current.params

努力改变

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

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

并且在您的重定向

console.log $route.current.params

关于AngularJS e2e 测试 - $routeParams 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19593679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com