我在我的应用程序中对路由器进行单元测试时遇到了一些麻烦,该应用程序是在Angular ui路由器上构建的.我想测试的是状态转换是否适当地改变了URL(稍后会有更复杂的测试,但这是我开始的地方.)
这是我的应用程序代码的相关部分:
angular.module('scrapbooks') .config( function($stateProvider){ $stateProvider.state('splash', { url: "/splash/", templateUrl: "/app/splash/splash.tpl.html", controller: "SplashCtrl" }) })
和测试代码:
it("should change to the splash state", function(){ inject(function($state, $rootScope){ $rootScope.$apply(function(){ $state.go("splash"); }); expect($state.current.name).to.equal("splash"); }) })
关于Stackoverflow(以及官方的ui路由器测试代码)的类似问题建议在$ apply中包装$ state.go调用应该足够了.但我已经这样做了,州仍然没有更新.$ state.current.name保持为空.