1

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.

Mauro
  • 3,946
  • 2
  • 27
  • 41

1 Answers1

2

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.

Dan Keiger
  • 671
  • 5
  • 12
  • where exactly do you do this? I'm trying to build a test for an existing codebase that uses $http and I can't get it to work. Could you post an example? – Mauro Sep 02 '15 at 17:51