Mauro
Mauro

Reputation: 4224

Karma Jasmine AngularJS testing with real HTTP requests

I want to test my code with real API calls (so I can test the API as well, and when I change the API I don't have to change the JS test as well, and a lot more benefits.) instead of the regular $httpBackend.expectPOST('http://api.com/login').response(200).

Essentially, I want to test a ProductsController that expects to be logged in through an AuthService.login() method and receive a list of products through ui-router's resolve feature.

In this case, the login method receives data that needs to be used to gather products.

Upvotes: 1

Views: 2096

Answers (1)

Dan Keiger
Dan Keiger

Reputation: 671

From the $httpBackend documentation found here: https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend

As opposed to unit-testing, in an end-to-end testing scenario or in scenario when an application is being developed with the real backend api replaced with a mock, it is often desirable for certain category of requests to bypass the mock and issue a real http request (e.g. to fetch templates or static files from the webserver). To configure the backend with this behavior use the passThrough request handler of when instead of respond

So, something like: $httpBackend.whenGET(/.*/).passThrough(); should suffice.

Upvotes: 2

Related Questions