我目前正在嘗試測試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 module Filter: upper should be able to uppercase an entire input FAILED Error: [$injector:unpr] Unknown provider: upperFilterProvider <- upperFilter http://errors.angularjs.org/1.4.7/ $injector/unpr?p0=upperFilterProvider%20%3C-%20upperFilter at C:/webroot/member_portal/app/bower_components/angular/angular.js:68:12 at 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) at C:/webroot/member_portal/app/bower_components/angular/angular.js:4294:45 at getService (C:/webroot/member_portal/app/bower_components/angular/angular.js:4437:39) at 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) Error: Declaration Location at window.inject.angular.mock.inject (C:/webroot/member_portal/app/bower_components/angular-mocks/angular-mocks.js:2409:25) at Suite. (C:/webroot/member_portal/app/home/home_test.js:766:20) at Suite. (C:/webroot/member_portal/app/home/home_test.js:763:5) at C:/webroot/member_portal/app/home/home_test.js:3:1 TypeError: upperFilter is not a function at Object. (C:/webroot/member_portal/app/home/home_test.js:771:20) Chrome 47.0.2526 (Windows 7 0.0.0): Executed 1 of 54 (1 FAILED) (skipped 53) ERROR (0.327 secs/0.005 secs)
您能否顯示一個例子。我試着改變beforeEach(module('myApp.home')); - > beforeEach(module('myApp'));還beforeEach(module('myApp.home')); - > beforeEach(module('myApp')); beforeEach(module('myApp.home'));這兩個導致錯誤:[$ injector:modulerr]由於: 錯誤:[$ injector:modulerr]無法實例化模塊myApp由於: 錯誤:[$ injector:nomod] Module' ngAnimate'不可用!您拼錯了模塊名稱或忘記加載模塊名稱。 –
由於'myApp'模塊中的依賴關係,此錯誤即將出現。要麼嘲笑這些依賴關係,要麼在另一個模塊中隔離過濾器,只需注入該模塊,或者可以在測試中包含這些依賴關係。 – jkris