【发布时间】:2014-10-09 21:08:11
【问题描述】:
所以我有我的代码:
(function (ng) {
ng.module('myModuleName')
.provider('myProviderName', ['importedThing', function (importedThing) {
//lots of cool stuff happens in here
}];
}])
.config(['$stateProvider', '$urlRouterProvider', 'importedThing', 'myProviderNameProvider', function ($stateProvider, $urlRouterProvider, importedThing, myProvider) {
window.stateProvider = $stateProvider;
$urlRouterProvider.otherwise(function ($injector, $location) {
$location.path("/pages");
});
}]);
}(angular));
我需要对这一行代码覆盖率进行单元测试:
$location.path("/" + myProvider.coolString);
但我不知道怎么做。我见过几个人用控制器来做这件事,但不是用提供者。
我的单元测试设置如下:
beforeEach(function () {
angular.module('dsLocale', function () { }).config(function ($urlRouterProvider) {
$urlRouterProvider.deferIntercept();
$urp = $urlRouterProvider;
$urp.deferIntercept();
});
});
//Lines are commented to represent random stuff I've tried already.
it('should call otherwise', function () {
//$urp.otherwise('/otherwise');
//$location.path("/lastrule");
console.log($location.path());
//local.$emit("$locationChangeSuccess");
expect(true).toBe(true);
});
我觉得它应该像将 $location.Path 更改为不存在的东西一样简单,所以 UIRouter 可以做到这一点,但它似乎并不那么容易。任何帮助表示赞赏,并提前感谢!
【问题讨论】:
标签: javascript angularjs unit-testing jasmine angular-ui-router