我無法在測試中使用inject
。AngularJS:在測試中注入造成錯誤
兩個angular
和angular-mocks
是version 1.3.14
我完全失去了這裏。任何想法將不勝感激!
這將導致一個錯誤:
describe("factory: Account", function() {
var $httpBackend;
var $rootScope;
var Account;
beforeEach(module('app'));
beforeEach(inject(function() {
// $httpBackend = _$httpBackend_;
}));
it("Should fetch account", function() {
});
});
但如果我刪除注入它會通過:
describe("factory: Account", function() {
var $httpBackend;
var $rootScope;
var Account;
beforeEach(module('app'));
beforeEach(function() {
// $httpBackend = _$httpBackend_;
});
it("Should fetch account", function() {
});
});
在Gruntfile.js:
karma: {
unit: {
options: {
frameworks: ['jasmine'],
singleRun: true,
browsers: ['Safari'],
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/moment/moment.js',
'bower_components/ckeditor/ckeditor.js',
'bower_components/ng-ckeditor/ng-ckeditor.min.js',
'app/app.js',
'app/**/*.js',
]
}
}
}
這裏的輸出測試:
Safari 8.0.5 (Mac OS X 10.10.3) factory: Account Should fetch account FAILED
/.../bower_components/angular/angular.js:63:32
/.../bower_components/angular/angular.js:4120:30
[email protected]/.../bower_components/angular/angular.js:323:24
[email protected]/.../bower_components/angular/angular.js:4081:12
[email protected]/.../bower_components/angular/angular.js:4007:22
[email protected]/.../bower_components/angular-mocks/angular-mocks.js:2353:60
這不是強制性的,只是爲了方便,如果你想再次使用具有相同名稱的變量,如你的例子 – Michiel 2017-02-10 10:57:27