在提供程序配置中单元测试$ urlRouterProvider.otherwise

时间:2014-10-09 21:08:12

标签: 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', '$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可以做到这一点,但它看起来并不那么容易。任何帮助表示赞赏,并提前感谢!

1 个答案:

答案 0 :(得分:0)

只是遇到了同样的问题。我想测试其他功能,但不是如何,我想出了一个方法,这对我有用。

我使用location.path设置路径,触发$ locationChangeSuccess事件然后它可以正常工作

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

没有事件,路径当然是/ nonExistentPath

- 这是我的完整测试文件。请注意,我使用的是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");
    });
});