【发布时间】:2015-07-22 08:42:52
【问题描述】:
我想通过对我的服务器的真实调用进行集成测试,所以,我不想使用 angular-mocks 中的 $httpBackend 模块,所以我试试这个:
beforeEach(inject(function($rootScope,_MembersDataSvc_){
service = _MembersDataSvc_;
}));
it('test',function(done){
service.me().then(function(){done();});
});
服务是:
function me() {
return $http
.get('urlBase/me')
.then(meSuccess);
function meSuccess(response) {
return response.data.members[0];
}
}
这从不调用 $http,似乎 angular-mocks 覆盖了 $http 服务并且从未调用过。
一些想法?
编辑 1:
据此贴:http://base2.io/2013/10/29/conditionally-mock-http-backend/
你可以为你不想模拟的 $http 调用做一个 passThrough,所以你试试这个:
var service;
var scope;
var $httpBackend;
beforeEach(inject(function($rootScope,_MembersDataSvc_,_$httpBackend_){
service = _MembersDataSvc_;
scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
}));
it('test',function(done){
//this.timeout(10000);
$httpBackend.whenGET(/views\/\w+.*/).passThrough();
$httpBackend.whenGET(/^\w+.*/).passThrough();
$httpBackend.whenPOST(/^\w+.*/).passThrough();
service.me().then(function(response){console.log(response);done();});
scope.$apply();
//service.getDevices(member).then(function(response){console.log(response);done();})
});
但是这里的 passThrough 是未定义的。
编辑 2:
我读了这篇文章:http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/,但我想那是一个独立的测试??,我想用 karma 和 jasmine 运行。
这是我的全部测试。
describe('integration test', function () {
beforeEach(function () {
module('MyAngularApp');
});
var service;
var scope;
var $httpBackend;
beforeEach(inject(function($rootScope,_MembersDataSvc_,_$httpBackend_){
service = _MembersDataSvc_;
scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
}));
it('test for test',function(done){
$httpBackend.whenGET(/views\/\w+.*/).passThrough();
$httpBackend.whenGET(/^\w+.*/).passThrough();
$httpBackend.whenPOST(/^\w+.*/).passThrough();
service.me().then(function(response){console.log(response);done();});
scope.$apply();
});
});
【问题讨论】:
-
您需要使用 E2E 测试中的部件docs.angularjs.org/api/ngMockE2E。另外,请查看 e2e 指南docs.angularjs.org/guide/e2e-testing
-
@Chandermani 问题是 E2E 使用 $http 模拟,我希望我的服务真正调用我的服务器,而不是模拟服务器。
-
这不是真的,你可以在E2E场景下进行真正的通话。模拟需要显式完成
-
@Chandermani 你有根据我上面的代码的例子吗?我正在尝试从 angular-mocks 评论 $httpBackend 提供程序,但是很脏。
标签: angularjs jasmine angularjs-http