我有一个AngularJS / Jasmine单元测试,适用于AngularJS 1.0.7,但在Angular 1.2.2中不起作用:
servicesSpec.js
describe('services', function() {
beforeEach(module('workPadApp'));
describe('taskGateway', function() {
var sut;
beforeEach(inject(function(taskGateway) {
sut = taskGateway;
}));
describe('saving a task', function() {
it('GETs correctly', inject(function($httpBackend) {
$httpBackend.expectGET('/').respond({ foo : 'bar' });
sut.createTask({ taskRef : 'baz' });
$httpBackend.flush();
}))
})
})
})
app.js
var workPadApp = angular.module('workPadApp', ['workPadApp.services']);
services.js
angular.module('workPadApp.services', []).
factory('taskGateway', function($http, $q) {
return {
createTask : function(task) {
$http.get('/')
.success(function(response) {
console.log('success');
})
.error(function(response) {
console.log('error');
});
}
}
});
预期结果
当我使用AngularJS 1.0.7使用Karma运行时,一切都很好:
日志:'成功'
Chrome 33.0.1750(Windows 7):执行1成功1次(0.179秒/0.03秒)
实际结果
但是,当我使用AngularJS 1.2.2运行它时,我得到了这个输出:
Chrome 33.0.1750(Windows 7)服务taskGateway保存任务GET正确失败
Error: No pending request to flush ! at Error (native) at Function.$httpBackend.flush (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/test/lib/angular/angular-mocks.js:1195:34) at null.<anonymous> (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/test/unit/servicesSpec.js:91:18) at Object.invoke (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/app/lib/angular.js:3641:28) at workFn (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/test/lib/angular/angular-mocks.js:1778:20) Error: Declaration Location at window.jasmine.window.inject.angular.mock.inject (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/test/lib/angular/angular-mocks.js:1764:25) at null.<anonymous> (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/test/unit/servicesSpec.js:72:26) at null.<anonymous> (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/test/unit/servicesSpec.js:62:3) at null.<anonymous> (c:/Users/Mark/Documents/Grean/HHM/Src/HHMFrontend/test/unit/servicesSpec.js:8:2)
Chrome 33.0.1750(Windows 7):执行1 of 1(1失败)(0.349秒/0.133秒)
我查看了documentation for $httpBackend,其中的示例使用$injector
服务使用了更为迫切的设置方式。
令我感到惊讶的是,因为版本1.0.7中的声明式样式非常简洁。这真的是未来唯一正确的方法,还是我错过了什么?
答案 0 :(得分:1)
正如turns out所示,相关的angular-*
模块必须是same version。
在此示例中,angular.js
和angular-mocks.js
必须是同一版本,1.2.2。