Angular Mock Backend
Angular Mock Backend is
- An AngularJS module to mock some (or all) server requests (optionally with delay) for backend-less development.
- 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.
Installation
npm install angular-mock-backend --save-dev
bower install angular-mock-backend --save-dev
Example
Backend-less Development
Include provided mock-angular.js
in your index.html
and use vinkaga.mockBackend
as your application dependency. Then include the following 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.
Protractor Tests
In your Protractor test script, insert the following code before tests
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
Mock Definitions
Array of individual mock definitions, where each mock definition itself is an array of
[config, delay, response]
config
Either an array or function as follows
[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 follows
Value | 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. |
Backend-less Development
For backend-less development, use mock definitions as follows
angular.module('vinkaga.mockBackend').constant('vinkaga.mockBackend.mock', <array of mock definitions>);
Protractor Tests
For Protractor tests, use mock definitions as follows
var mock = require('angular-mockBackend');
mock.mock(<array of mock definitions>);