使用 Jasmine 测试 Angular 路由过滤器。代码有效但无法通过测试
Testing Angular route filter with Jasmine. Code works but cant make test pass
所以我的路由过滤器按预期工作,我正在围绕它编写一些测试。我有几个通过的测试,但出于某种原因我无法通过这个测试。我的路由过滤器如下所示:
stripUs: ->
resolve: ->
resolution: ($location) ->
urlParts = $location.$$path.split("/")
if urlParts.indexOf('us') is 1
$location.path(urlParts.slice(2,urlParts.length).join("/"))
想法是将 /us/foo/bar 网址重定向到 /foo/bar。
我目前为这个过滤器通过的测试是:
ddescribe 'stripUs', ->
location = rootScope = null
beforeEach inject ($location, $rootScope, initialDataService, ignoreHttpBackend) ->
ignoreHttpBackend()
location = $location
rootScope = $rootScope
it 'removes /us from /us/programming', ->
location.path("/us/programming")
rootScope.$digest()
expect(location.path()).toEqual('/programming')
it 'removes /us from /us/programming/**', ->
location.path("/us/programming/sports")
rootScope.$digest()
expect(location.path()).toEqual('/programming/sports')
it 'preserves route params', ->
location.path("/us/programming/sports?affiliate=foo&&process=foobarred")
rootScope.$digest()
expect(location.path()).toEqual('/programming/sports?affiliate=foo&&process=foobarred')
我无法通过的测试是:
it 'preserves route params', ->
location.path("/us/programming?affiliate=foo")
rootScope.$digest()
expect(location.path()).toEqual('/programming?affiliate=foo')
错误信息是:
Expected '/us/programming?affiliate=foo' to equal '/programming?affiliate=foo'
这会让我相信代码不起作用,但如果我真的尝试访问该页面,它就会起作用。此外,当我尝试将控制台日志放在路由过滤器的最顶部时,日志永远不会被命中。我是 Jasmine 测试的新手,可以使用任何可能的帮助。提前致谢。
我通过改变期望解决了这个问题。我认为我的测试结果更好,但我一直不明白为什么我无法通过 运行 的测试。这是我将测试更改为的内容。
it 'preserves route params', ->
location.path("/us/programming/sports?affiliate=bestbuy&process=foobarred")
rootScope.$digest()
expect(location.path()).toContain('?affiliate=bestbuy&process=foobarred')
it 'preserves route params', ->
location.path("/us/programming?affiliate=bestbuy")
rootScope.$digest()
expect(location.path()).toContain('?affiliate=bestbuy')
所以我的路由过滤器按预期工作,我正在围绕它编写一些测试。我有几个通过的测试,但出于某种原因我无法通过这个测试。我的路由过滤器如下所示:
stripUs: ->
resolve: ->
resolution: ($location) ->
urlParts = $location.$$path.split("/")
if urlParts.indexOf('us') is 1
$location.path(urlParts.slice(2,urlParts.length).join("/"))
想法是将 /us/foo/bar 网址重定向到 /foo/bar。
我目前为这个过滤器通过的测试是:
ddescribe 'stripUs', ->
location = rootScope = null
beforeEach inject ($location, $rootScope, initialDataService, ignoreHttpBackend) ->
ignoreHttpBackend()
location = $location
rootScope = $rootScope
it 'removes /us from /us/programming', ->
location.path("/us/programming")
rootScope.$digest()
expect(location.path()).toEqual('/programming')
it 'removes /us from /us/programming/**', ->
location.path("/us/programming/sports")
rootScope.$digest()
expect(location.path()).toEqual('/programming/sports')
it 'preserves route params', ->
location.path("/us/programming/sports?affiliate=foo&&process=foobarred")
rootScope.$digest()
expect(location.path()).toEqual('/programming/sports?affiliate=foo&&process=foobarred')
我无法通过的测试是:
it 'preserves route params', ->
location.path("/us/programming?affiliate=foo")
rootScope.$digest()
expect(location.path()).toEqual('/programming?affiliate=foo')
错误信息是:
Expected '/us/programming?affiliate=foo' to equal '/programming?affiliate=foo'
这会让我相信代码不起作用,但如果我真的尝试访问该页面,它就会起作用。此外,当我尝试将控制台日志放在路由过滤器的最顶部时,日志永远不会被命中。我是 Jasmine 测试的新手,可以使用任何可能的帮助。提前致谢。
我通过改变期望解决了这个问题。我认为我的测试结果更好,但我一直不明白为什么我无法通过 运行 的测试。这是我将测试更改为的内容。
it 'preserves route params', ->
location.path("/us/programming/sports?affiliate=bestbuy&process=foobarred")
rootScope.$digest()
expect(location.path()).toContain('?affiliate=bestbuy&process=foobarred')
it 'preserves route params', ->
location.path("/us/programming?affiliate=bestbuy")
rootScope.$digest()
expect(location.path()).toContain('?affiliate=bestbuy')