$ configprovider的jasmine测试在config块中解析

时间:2014-06-04 05:54:48

标签: angularjs unit-testing karma-jasmine route-provider

我的模块配置块中有以下内容:

var appModule = angular.module('myApp',['ngRoute'])
.config(['$httpProvider', '$routeProvider', '$locationProvider', '$translateProvider', function ($httpProvider, $routeProvider, $locationProvider, $translateProvider) {

    $locationProvider.html5Mode(true)

    $routeProvider
        .when('/services/main', {templateUrl: '/services/main/html/main.html', controller: 'MainCtrl', resolve: {
            myVar: function (varService) {
                return varService.invokeService();
            }
        }})
}])

规范文件:

describe("Unit Testing: config - ", function() {

var appModule;
var routes;

beforeEach(function() {
    appModule = angular.module("myApp");
});

it('should test routeProvider', function() {

      inject(function($route, $location, $rootScope) {
          routes = $route;
      });
    }); 
});

然而,在运行测试时,我收到以下错误:

Unit Testing: config -  should test routeProvider FAILED
Error: [$injector:unpr] http://errors.angularjs.org/1.2.15/$injector/unpr?p0=%24routeProvider%20%3C-%20%24route
    at Error (native)

我的karma配置包含angular-route.min.js文件。任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

使用angular.mock.module而不是angular.module解决了这个问题。

appModule = angular.mock.module("myApp");

我发现我们应该使用mock来注入模块,它还包括配置以及模块用于注册新模块的位置。因此,如果有人引用已经注册的模块,我们应该使用angular.mock.module。

文件说:

This function registers a module configuration code. It collects the configuration information which will be used when the injector is created by inject.