Angularjs 具有无效路由的单元测试ui路由器

Angularjs 具有无效路由的单元测试ui路由器,angularjs,unit-testing,angular-ui-router,Angularjs,Unit Testing,Angular Ui Router,使用这一有助于为ui路由器添加单元测试的方法,我添加了以下测试: describe 'State: myApp', -> # load the filter's module beforeEach module 'myApp' $rootScope = $state = {} beforeEach inject ['$rootScope', '$state', (_$rootScope_, _$state_) -> $rootScope = _$rootS

使用这一有助于为ui路由器添加单元测试的方法,我添加了以下测试:

describe 'State: myApp', ->

  # load the filter's module
  beforeEach module 'myApp'

  $rootScope = $state = {}
  beforeEach inject ['$rootScope', '$state', (_$rootScope_, _$state_) ->
    $rootScope = _$rootScope_
    $state = _$state_
    ]
此测试成功,因为访问主状态会导致命中/url

然而,当我测试一个无效的路由时,我得到null。测试成功:

  it "should return the '/' url if a URL cannot be constructed", ->
    expect($state.href('FOO_BAR_BAZ')).toEqual null
考虑这些:

注意:如果无法构造有效的url,则返回null

但是,我的应用程序包含以下代码,用于将无效状态重新路由到/:

因此,此测试是否仍应返回null?或者它应该返回/?

您的

$urlRouterProvider.otherwise '/'
确实是在让它回归/

您可能希望通过侦听$rootScope broadcast$statechangererror来处理无效URL,并将默认设置删除为/:

将默认设置用于/与以下内容一起使用:

$urlRouterProvider.otherwise '/'
我认为你不能同时拥有这两个

$urlRouterProvider.otherwise '/'
$rootScope.$on('$stateChangeError', function(){})
$urlRouterProvider.otherwise '/'