【问题标题】:jasmine test for $routeprovider resolve in config block配置块中 $routeprovider 的茉莉花测试解析
【发布时间】:2014-06-04 05:54:47
【问题描述】:

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

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 文件。任何建议都会有所帮助。

【问题讨论】:

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


    【解决方案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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 2017-09-27
      相关资源
      最近更新 更多