如何使用jasmine和angular-mocks测试$ http而不用mock?

cma*_*o01 7 jasmine angularjs angularjs-http

我想对我的服务器进行真正的调用进行集成测试,所以,我不想使用angular-mocks的$ httpBackend模块,所以我试试这个:

beforeEach(inject(function($rootScope,_MembersDataSvc_){
    service = _MembersDataSvc_;
}));

it('test',function(done){
    service.me().then(function(){done();});
});
Run Code Online (Sandbox Code Playgroud)

服务是:

function me() {
      return $http
        .get('urlBase/me')
        .then(meSuccess);

        function meSuccess(response) {
            return response.data.members[0];
        }
    }
Run Code Online (Sandbox Code Playgroud)

这从来没有调用$ 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();})
    });
Run Code Online (Sandbox Code Playgroud)

但是passThrough在这里是未定义的.

编辑2:

我读过这篇文章:http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/,但我认为这是一个独立的测试??,我想用业力和茉莉花.

这是我的全部测试.

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();
    });
});
Run Code Online (Sandbox Code Playgroud)

kul*_*lak 0

我建议使用 ngMidwayTester,它允许您连接到真正的后端,我用它在代码级别进行集成测试 - 所以介于单元和 e2e 测试之间:

AngularJS 中的两种类型的测试(另加一种) - 使用 AngularJS 和 Karma 进行全谱测试