AngularJS Mock Backend for Backend-less Development and Protractor Tests
npm install angular-mock-backend
Angular Mock Backend is
1. An AngularJS module to mock some (or all) server requests (optionally with delay) for backend-less development.
2. A Protractor module to help test client by mocking some (or all) server requests (optionally with delay).
This module provides functionality somewhat similar to that provided by ngMockE2E $httpBackend. But it does not use ngMockE2E $httpBackend for its implementation.
sh
npm install angular-mock-backend --save-dev
bower install angular-mock-backend --save-dev
`
Example
$3
Include provided `mock-angular.js` in your `index.html` and use `vinkaga.mockBackend` as your application dependency. Then include the following JavaScript
`JavaScript
var mocks = [
[['get', '/users', undefined, undefined, undefined], 200, {data:[
{firstName: 'john', lastName: 'doe'},
{firstName: 'angular', lastName: 'js'}
]}]
];
angular.module('vinkaga.mockBackend').constant('vinkaga.mockBackend.mock', mocks);
`
This will mock GET for `/users` with the specified data and 200ms delay. For a full working example, see example-backend-less.
$3
In your Protractor test script, insert the following code before tests
`JavaScript
var mock = require('angular-mockBackend');
mock.mock([
[['get', '/users', undefined, undefined, undefined], 200, {data:[
{firstName: 'john', lastName: 'doe'},
{firstName: 'angular', lastName: 'js'}
]}],
]);
`
This will mock GET for `/users` with the specified data and 200ms delay in Protractor tests. For a full working example, see example-protractor.
API
$3
Array of individual mock definitions, where each mock definition itself is an array of
`JavaScript
[config, delay, response]
`
#### config
Either an array or function as follows
`JavaScript
[method, url, params, data, headers]
// or
function(method, url, params, data, headers) {...}
`
If config definition is a function, the third parameter of mock definition (`response`) is ignored. Instead the value returned by the function is used in place of `response`.
#### delay
A number in milliseconds. Negative numbers will have an unpredictable effect.
#### response
If `config` is specified as a function, it's the value returned by the `config` function. Otherwise, it is the third parameter of the mock definition. It is treated as followsValue | Description
------------- | -------------
object | Matching requests are mocked and delay is applied. The returned data is
`response.data` and the returned status is `response.status`.
truthy but not object | Matching requests are passed to the server after applying the delay.
falsy | Matching requests are not affected.$3
For backend-less development, use mock definitions as follows
`JavaScript
angular.module('vinkaga.mockBackend').constant('vinkaga.mockBackend.mock', );
`
$3
For Protractor tests, use mock definitions as follows
`JavaScript
var mock = require('angular-mockBackend');
mock.mock();
``