【发布时间】:2016-08-25 22:22:34
【问题描述】:
以下是我的模块和单元测试设置。当我尝试设置 $state 时,$injector.get("$state") 会抛出总是很有趣的 Unknown provider: $stateProvider 错误,我不明白为什么。
angular.module("my-module", [
//Third Party
"ui.bootstrap",
"ui.router",
"toaster",
"angular-loading-bar",
"ngAnimate",
"ngSanitize",
"ApplicationInsightsModule",
"pascalprecht.translate"
]);
describe("something descriptive and helpful", (): void => {
// a bunch of other service variables....
var $state: any;
// I've tried with and without this line
beforeEach(() => angular.module("ui.router"));
beforeEach(() => {
angular.module("my-module");
inject(($injector: ng.auto.IInjectorService): void => {
// a bunch of other service variable assignments
$state = $injector.get("$state");
});
});
【问题讨论】:
-
还应该注意的是,在规范中使用真正的路由器是很尴尬的。尽可能坚持使用 $stateProvider/$state 模拟。
-
@estus 所以,如果我必须模拟我的整个路线配置......测试它有什么意义?那时我实际上正在重写代码,对吗?似乎不值得。
标签: angularjs unit-testing angular-ui-router