How can I implement a mock back-end to do quick prototyping with AngularJS?
I need to be able to fake response delay, response data and the like.
I use the $http service.
How can I implement a mock back-end to do quick prototyping with AngularJS?
I need to be able to fake response delay, response data and the like.
I use the $http service.
You can use $httpBackend
Here's an example from the AngularJS website:
phones = [{name: 'phone1'}, {name: 'phone2'}];
// returns the current list of phones
$httpBackend.whenGET('/phones').respond(phones);
Just to add to these answers: take a look at https://github.com/mdasberg/ng-apimock
and https://www.npmjs.com/package/open-api-test-generator
ng-apimock is a scenario switcher for protractor and development, and open-api-test-generator can generate mocks for this tool. Pretty handy stuff once you get to know the modules
For an alternative more automated way of backend mocking, take a look at swagger-api-mock that works just great together with rest-json. These modules together can be used to generate $httpBackend responses: rest-json.js as http-like access to json data and swagger-api-mock/lib/mock-data.js as generator for the json mock data objects based on swagger (or json schema) definitions
You can use angular mocks to provide a mock back-end.
Working demo on plnkr.
Basically you include angular-mocks after angular, and use the code provided in this gist and you will be able to control both requests and responses, including headers and fake response delays etc.
Example:
```
You can also set up urls that should pass trough to a actual server too (check passThrough())