javascript - 在提供程序配置中对 $urlRouterProvider.otherwise 进行单元测试

标签 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);

但是我不知道怎么办。我见过几个人使用 Controller 来做到这一点,但没有使用提供者。

我的单元测试设置如下:

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("/");
    });

没有事件,路径当然是/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");
    });
});

关于javascript - 在提供程序配置中对 $urlRouterProvider.otherwise 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26287938/

相关文章:

c++ - 使用 CMake 进行单元测试,无需重复编译

javascript - 鼠标悬停时弹出 Jquery 图像

javascript - nodejs mongodb不根据id删除

javascript - 当它被覆盖时,如何检测哪个图形(圆形,多边形)发生一些事件?谷歌地图 API JavaScript

javascript - vuetifyjs : Adding only used icons to build

javascript - Ionic AngularJS 所有 ng-click 事件都触发两次?

javascript - 从 ng-change 获取文件

javascript - ngTable tableParams count vlue 被忽略

javascript - 编写 "unit testable"jQuery 代码?

spring - MockMvc 测试 Spring 抛出 org.springframework.web.HttpMediaTypeNotSupportedException