如何在单元测试中获得状态?

时间:2016-08-24 17:16:18

标签: angularjs typescript angular-ui-router

这是路由的配置文件。

module SalesDashboard.Administration.Routing {

"use strict";

class RoutingConfig {

    static $inject = ["$stateProvider", "$urlRouterProvider", "$locationProvider"];

    constructor($stateProvider, $urlRouterProvider, $locationProvider) {

        $locationProvider.html5Mode(true);

        $stateProvider.
            state("main", {
                url: "/index.html"
            }).
            state("home", {
                url: "/administration/home",
                templateUrl: "components/home/home.html"
            });

       $urlRouterProvider.otherwise("/administration/home");
    }
}

angular
    .module("SalesDashboard.Administration")
    .config(RoutingConfig)
    .run(["$rootScope", "$log", ($rootScope: ng.IRootScopeService, $log: ng.ILogService) => {
        $rootScope.$on("$stateNotFound", (event, toState, toParams, fromState, fromParams, error) => {
            $log.log(event);
            $log.log(error);
        });
        $rootScope.$on("$stateChangeError", (event, toState, toParams, fromState, fromParams, error) => {
            $log.log(event);
            $log.log(error);
        });
        $rootScope.$on("$stateChangeStart", (event, toState, toParams, fromState, fromParams, error) => {
            $log.log(event);
            $log.log(error);
        });
    }]);
}

这是规范文件

describe("SalesDashboard.Administration.Routing.RoutingConfig", (): void => {

    var $httpBackend: ng.IHttpBackendService;
    var $q: ng.IQService;
    var $rootScope: ng.IRootScopeService;
    var $state: ng.ui.IState;
    var $stateService: ng.ui.IStateService;
    var $timeout: ng.ITimeoutService;
    var $location: ng.ILocationService;
    var $templateCache: ng.ITemplateCacheService;

    beforeEach(() => {

        angular.mock.module(($provide: ng.auto.IProvideService): void => {
            $provide.constant("APP_LANG", { someUrl: "en-US" });
        });

        angular.mock.module("SalesDashboard.Administration");

        inject(($injector: ng.auto.IInjectorService): void => {
            $httpBackend = $injector.get("$httpBackend") as ng.IHttpBackendService;
            $q = $injector.get("$q") as ng.IQService;
            $rootScope = $injector.get("$rootScope") as ng.IRootScopeService;
            $state = $injector.get("$state") as ng.ui.IState;
            $stateService = $injector.get("$state") as ng.ui.IStateService;
            $timeout = $injector.get("$timeout") as ng.ITimeoutService;
            $templateCache = $injector.get("$templateCache") as ng.ITemplateCacheService;
            $location = $injector.get("$location") as ng.ILocationService;
        });

        $httpBackend.when("GET", "/assets/languages/locale-en-us.json").respond(null);
    });
        // some tests
    });

我遇到的问题是在进行测试时,没有任何一种状态存在,我无法为我的生活找出原因。如果我执行$ stateService.go(" main"),我收到错误消息无法解析' main'来自州' 。最大的问题是:如何让我的状态配置存在于我的测试环境中?

0 个答案:

没有答案