【问题标题】:Unit testing $urlRouterProvider.otherwise in provider config在提供者配置中对 $urlRouterProvider.otherwise 进行单元测试
【发布时间】: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


    【解决方案1】:

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

    我正在使用 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");
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 2021-11-24
      • 2023-01-19
      • 2020-05-01
      • 2013-09-29
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多