【问题标题】:AngularJS routing test Unknown providerAngularJS路由测试未知提供者
【发布时间】:2018-05-19 00:21:18
【问题描述】:

我正在尝试像这样在路由器上设置茉莉花测试

it('should map routes to controllers and templates', function() {

    inject(function($route) {
        module('igt');

        expect($route.routes['/'].controller).toBe('mainPageCtrl');
        expect($route.routes['/'].templateUrl).toEqual('html/pages/main.html');

        // otherwise redirect to
        expect($route.routes[null].redirectTo).toEqual('/')
    });

});

我的路由器文件是(只是开始,因为它很长):

(function () {
    'use strict';

    angular
        .module('igt')
        .config(configure);

    configure.$inject = ['$routeProvider', '$httpProvider'];

    function configure($routeProvider, $httpProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'html/pages/main.html',
                controller: 'mainPageCtrl'
            })

我确实在 karma.conf.js 文件中注入了这些文件:

// list of files / patterns to load in the browser
files: [
    '../../node_modules/angular/angular.js',
    '../../node_modules/angular-mocks/angular-mocks.js',
    '../app.js',
    '../router.js',
    'unit/*.js'
],

现在当我用 karma start 开始测试时,它给了我错误:

错误:[$injector:unpr] 未知提供者:$routeProvider

我的整个 JS 代码都封装在 IIFE 中,所以我连一个全局变量都没有(我不知道这是否重要)。

为什么我有这个错误我做错了什么?

【问题讨论】:

    标签: angularjs testing jasmine karma-runner router


    【解决方案1】:

    改成

    describe('Testing Routes', function () {
    beforeEach(module('myApp'));
    it('should map routes to controllers and templates', function($route) {
      expect($route.routes['/'].controller).toBe('mainPageCtrl');
    

    【讨论】:

    • 现在我有PhantomJS 2.1.1 (Windows 8 0.0.0) Testing Routes should map routes to controllers and templates FAILED TypeError: undefined is not an object (evaluating '$route.routes['/']') in unit/testAngularUnitSpec.js (line 4)
    • 发生了什么事?
    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    相关资源
    最近更新 更多