Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 提供程序配置中的单元测试$urlRouterProvider.OR_Javascript_Angularjs_Unit Testing_Jasmine_Angular Ui Router - Fatal编程技术网

Javascript 提供程序配置中的单元测试$urlRouterProvider.OR

Javascript 提供程序配置中的单元测试$urlRouterProvider.OR,javascript,angularjs,unit-testing,jasmine,angular-ui-router,Javascript,Angularjs,Unit Testing,Jasmine,Angular Ui Router,所以我有我的代码: (function (ng) { ng.module('myModuleName') .provider('myProviderName', ['importedThing', function (importedThing) { //lots of cool stuff happens in here }]; }]) .config(['$stateProvider',

所以我有我的代码:

(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就可以做它该做的事情,但看起来并没有那么简单。感谢您的帮助,并提前表示感谢

也有同样的问题。我想测试其他功能,但不知道如何,我想出了一个方法,这对我来说是可行的

我正在使用location.path设置路径,启动$locationChangeSuccess事件,然后它就可以工作了

it('redirects to otherwise page after locationChangeSuccess', function() {
        location.path('/nonExistentPath');
        rootScope.$emit("$locationChangeSuccess");
        expect(location.path()).toBe("/");
    });
如果没有该事件,则路径为当然路径/不存在路径

-- 这是我的完整测试文件。请注意,我使用的是browserify,文件可能与您的有点不同

    'use strict';

require('../../../app');

var objectToTest = 'CoreRouterConfig';

describe(objectToTest, function () {
    var rootScope,
        state,
        location;

    beforeEach(angular.mock.module('ui.router'));
    beforeEach(angular.mock.module('core'));

    beforeEach(inject(function ($rootScope, $state, $location) {
        rootScope = $rootScope;
        state = $state;
        location = $location;
    }));

    it('should respond home state', function() {
        expect(state.href('home')).toEqual('#/');
    });

    it('init path is correct', function() {
        rootScope.$emit("$locationChangeSuccess");
        expect(location.path()).toBe("/");
    });

    it('redirects to otherwise page after locationChangeSuccess', function() {
        location.path('/nonExistentPath');
        rootScope.$emit("$locationChangeSuccess");
        expect(location.path()).toBe("/");
    });

    it('does not redirect to otherwise page without locationChangeSuccess', function() {
        location.path('/nonExistentPath');
        expect(location.path()).toBe("/nonExistentPath");
    });
});