【问题标题】:Unit Testing ui-router with Invalid Route使用无效路由对 ui-router 进行单元测试
【发布时间】:2014-06-06 18:53:32
【问题描述】:

使用这个有用的 post 为 ui-router 添加单元测试,我添加了以下测试:

describe 'State: myApp', ->

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

  $rootScope = $state = {}
  beforeEach inject ['$rootScope', '$state', (_$rootScope_, _$state_) ->
    $rootScope = _$rootScope_
    $state = _$state_
    ]

此测试成功,因为访问home 状态会导致访问/ url。

  it "should return the '/' url for the 'home' state", ->
    expect($state.href('home')).toEqual '/'

但是,当我测试无效路由时,我得到null。测试成功:

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

考虑这些docs

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

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

$urlRouterProvider.otherwise '/'

因此,该测试是否仍应返回null?还是应该返回/

【问题讨论】:

    标签: angularjs unit-testing angular-ui-router


    【解决方案1】:

    你的

    $urlRouterProvider.otherwise '/'
    

    确实让它返回/

    您可能希望通过收听 $rootScope 广播 $stateChangeError 来处理无效的 url,并删除您对 / 的默认设置:

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

    使用默认的 / with:

    $urlRouterProvider.otherwise '/'
    

    我认为你不能同时拥有两者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 2020-12-30
      • 1970-01-01
      • 2020-03-22
      相关资源
      最近更新 更多