7

I want to make an integration test with real calls to my server, so, I don't want to use the $httpBackend module from angular-mocks, So I try this:

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

it('test',function(done){
    service.me().then(function(){done();});
});

And the service is:

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

        function meSuccess(response) {
            return response.data.members[0];
        }
    }

This never call the $http, it seems that angular-mocks override the $http service an never made the call.

Some ideas?

EDIT 1:

According to this post: http://base2.io/2013/10/29/conditionally-mock-http-backend/

you can make a passThrough for that $http calls that you don't want to mock, so y try this:

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();})
    });

But the passThrough is undefined here.

EDIT 2:

I read this post: http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/, but I supose that is an stanalone test??, I want to run with karma and jasmine.

This is my entire test.

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();
    });
});
rave
  • 1,022
  • 1
  • 12
  • 23
cmarrero01
  • 691
  • 7
  • 21
  • You need to use parts from E2E testing https://docs.angularjs.org/api/ngMockE2E. Also, look at e2e guide https://docs.angularjs.org/guide/e2e-testing – Chandermani May 12 '15 at 04:34
  • @Chandermani the thing is that E2E use mocks for $http, I want to that my service make the real call to my server, not the mock one. – cmarrero01 May 12 '15 at 21:02
  • That is not true, you can make real calls in E2E scenario. Mocking needs to be done explicitly – Chandermani May 13 '15 at 04:32
  • @Chandermani do you have an example according to my code above?. I'm try commenting the $httpBackend provider from angular-mocks, but is dirty. – cmarrero01 May 13 '15 at 12:48
  • See this post http://www.base2.io/2013/10/29/conditionally-mock-http-backend/ – Chandermani May 13 '15 at 14:48
  • @Chandermani, I try to use passThrough but is undefined, on this line: $httpBackend.whenGET(/^\w+.*/).passThrough(); TypeError: undefined is not a function And that post is not much to do with my real question, I don't want mocking anything, I want that my test make the real calls of http and listen the callbacks of each $http call. – cmarrero01 May 13 '15 at 19:34
  • There are breaking changes required to use $httpBackend from ngMockE2E. I hope you are not including the `ngMock` module during E2E testing. This module will override the default $http service. In E2E tests standard http calls work. – Chandermani May 14 '15 at 03:40
  • The post i gave was not complete, sorry for that, a better treatment on this subject is available here http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/. – Chandermani May 14 '15 at 03:42
  • I'm using karma and jasmine for run the test, I'll edit my post with the entire test code. I don't include ngMock implicity, but, I supouse that is injected by defaullt. – cmarrero01 May 14 '15 at 11:22
  • Karma is for unit testing, not for E2E testing. For E2E testing you need to use Protractor. A remote request is not allowed in unit test. – Chandermani May 14 '15 at 11:54

1 Answers1

0

I recomend using ngMidwayTester that allows you to connect to the real backend, I use it to make integration tests on the code level - so something in between unit and e2e testing:

Two types of tests in AngularJS (plus one more) - Full-Spectrum Testing with AngularJS and Karma

kulak
  • 852
  • 7
  • 16