角度测试未知提供者

时间:2015-12-28 14:16:40

标签: javascript angularjs node.js unit-testing

我目前正在尝试测试view1(home.js),在view1中,我使用了从view1_test.js移动到app.js级别的过滤器和指令。我的所有测试都用来传递,现在任何引用过滤器或指令的东西现在都无法通过测试。我想知道我需要创建一个app_test.js还是有办法修复测试仍然有效?以下是我所说的一个例子。

app.js

'use strict';

// Declare app level module which depends on views, and components
angular.module('myApp', [
    'ngRoute',
    'myApp.home',
    'ngAnimate',
    'ui.bootstrap',
    'ui.date'
]).
config(['$routeProvider','$httpProvider','$locationProvider', function($routeProvider) {
    $routeProvider.otherwise({redirectTo: '/home'});
}])
.filter('upper', function () {
    return function (input) {
        return input.toUpperCase();
    };
})

view1.js(home.js)

'use strict';

angular.module('myApp.home', ['ngRoute'])
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/home', {
        templateUrl: 'home/home.html',
        controller: 'HomeCtrl'
    });
}])

view1_test.js(home_test.js)

'use strict';
describe('myApp.home module', function () {
    beforeEach(module('myApp.home'));
}

describe('Filter: upper', function () {
    var upperFilter;

    beforeEach(inject(function (_upperFilter_) {
        upperFilter = _upperFilter_;
    }));

    it('should be able to uppercase an entire input', function () {
        expect(upperFilter('hello')).toBe('HELLO');
        expect(upperFilter('hello world')).toBe('HELLO WORLD');
    });

});

运行此测试时。我明白了:

  

Chrome 47.0.2526(Windows 7 0.0.0)myApp.home模块过滤器:上层应该能够大写整个输入FAILED           错误:[$ injector:unpr]未知提供者:upperFilterProvider< - upperFilter           http://errors.angularjs.org/1.4.7/ $注射器/ unpr?P0 = upperFilterProvider%20%3 C-%20upperFilter               在C:/webroot/member_portal/app/bower_components/angular/angular.js:68:12               在C:/webroot/member_portal/app/bower_components/angular/angular.js:4289:19               at Object.getService [as get](C:/webroot/member_portal/app/bower_components/angular/angular.js:4437:39)               在C:/webroot/member_portal/app/bower_components/angular/angular.js:4294:45               在getService(C:/webroot/member_portal/app/bower_components/angular/angular.js:4437:39)               在Object.invoke(C:/webroot/member_portal/app/bower_components/angular/angular.js:4469:13)               at Object.workFn(C:/webroot/member_portal/app/bower_components/angular-mocks/angular-mocks.js:2438:20)           错误:声明位置               在window.inject.angular.mock.inject(C:/webroot/member_portal/app/bower_components/angular-mocks/angular-mocks.js:2409:25)               在套房。 (C:/webroot/member_portal/app/home/home_test.js:766:20)               在套房。 (C:/webroot/member_portal/app/home/home_test.js:763:5)               在C:/webroot/member_portal/app/home/home_test.js:3:1           TypeError:upperFilter不是函数               在对象。 (C:/webroot/member_portal/app/home/home_test.js:771:20)   Chrome 47.0.2526(Windows 7 0.0.0):执行1/54(1失败)(跳过53)错误(0.327秒/ 0.005秒)

1 个答案:

答案 0 :(得分:1)

您为每个文件声明了2个不同的模块

app.js -> angular.module('myApp', [ ...

view1.js -> angular.module('myApp.home', ['ngRoute'])

在您的home_test.js中,您应该注入myApp模块而不是myApp.home,因为您已将其移至该模块