How to test $http without mock with jasmine and angular-mocks?
Asked Answered
B

1

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();
    });
});
Baseburner answered 11/5, 2015 at 18:1 Comment(10)
You need to use parts from E2E testing docs.angularjs.org/api/ngMockE2E. Also, look at e2e guide docs.angularjs.org/guide/e2e-testingWhiplash
@Whiplash 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.Baseburner
That is not true, you can make real calls in E2E scenario. Mocking needs to be done explicitlyWhiplash
@Whiplash do you have an example according to my code above?. I'm try commenting the $httpBackend provider from angular-mocks, but is dirty.Baseburner
See this post base2.io/2013/10/29/conditionally-mock-http-backendWhiplash
@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.Baseburner
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.Whiplash
The post i gave was not complete, sorry for that, a better treatment on this subject is available here blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e.Whiplash
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.Baseburner
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.Whiplash
F
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

Flatcar answered 19/11, 2015 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.