How to check $ http without layout with jasmine and angular -mocks?

I want to do an integration test with real calls on 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 service:

 function me() { return $http .get('urlBase/me') .then(meSuccess); function meSuccess(response) { return response.data.members[0]; } } 

This never calls $ http, it seems that angular modules override the $ http service and never call.

Some ideas?

EDIT 1:

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

you can passThrough for these $ http calls you don’t want to mock, so 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 passThrough is undefined here.

EDIT 2:

I read this post: http://blog.xebia.com/2014/03/08/angularjs-e2e-testing-using-ngmocke2e/ , but I suppose this is a stanalone test, I want to work with karma and jasmine.

This is all my 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(); }); }); 
+6
source share
1 answer

I recommend using ngMidwayTester, which allows you to connect to a real server, I use it for integration tests at the code level - so there is something between unit testing and e2e:

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

0
source

Source: https://habr.com/ru/post/986988/


All Articles