Question

I am developing an Angular app (Angular 1.1.5). I need some way of mocking REST API responses for e2e tests.

AngularJS (Green & Seshadri) goes into some detail on unit testing HTTP requests with $httpBackend, but has little to say about e2e tests.

The documentation suggests using the ngMockE2E module to mock $httpBackend (which is distinct from the normal unit testing $httpBackend mock.

I spent a few hours investigating the ngMockE2EAPI and, whilst I can see some benefits, there seem to be many downsides. My question is:

What are the advantages of ngMockE2E over a simple server that provides test responses? or, more generally, What is the best way to test an Angular app's interactions with a server?

I can think of a few disadvantages:

  • I need to change my HTML to bootstrap the app with a different Angular module (a new one that depends on ngMockE2E and my actual application module)
  • There are no HTTP requests for Firebug/Developer tools to inspect. This is a big one for me. I ended up writing request/response logging methods in my mock backend. I guess most mock back ends contain this kind of code.
  • There isn't an easy way to extract parameters from URLs. If I wanted to the get the id parameter from /resource/:id, I'd need to give a regex to match the URL (\/resource\/.+$), then split the matching URL string on slash and pick the last element.
  • The API is clunky. Returning a static object is simple, but dynamically determining which test object to return based on the requested URL is difficult. Some apps may be able to hard-code a complete set of test URLs, but I need to test a large number of resources so this is impractical for me. More complex code means there is more to go wrong.

I can also think of some advantages. When using ngMockE2E:

  • I don't need to tell my app where the test API is - $httpBackend will inspect all HTTP requests for matches.
  • I don't need to run a web server at all.
  • I don't need to restart a server to pick up changes to my test data.
  • There are fewer overall components in the tests. However, given that this is an integration test, this isn't a huge benefit.

Very interested in thoughts and experiences.

Was it helpful?

Solution

My conclusion, several weeks of development later: I found a simple dummy backend (Node/Express) more flexible and maintanable than the e2e testing mock backend. The most compelling reasons are proper URL pattern matching and real HTTP requests for browser debugging.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top