httpbackend
v2.0.0
Published
Module for mock http request (http backend) with protractor for testing angular app
Downloads
1,388
Readme
Http Backend
Http backend mock module for protractor
Installation
npm install httpbackend
include angular mock script https://github.com/angular/bower-angular-mocks
Simple Usage
var HttpBackend = require('httpbackend');
var backend = null;
describe('Test Http backend methods', function() {
beforeEach(function() {
backend = new HttpBackend(browser);
});
afterEach(function() {
backend.clear();
});
it('Test whenGET with string response', function() {
backend.whenGET(/result/).respond('raoul');
browser.get('http://127.0.0.1:8080');
var result = element(by.binding('result'));
expect(result.getText()).toEqual('raoul');
});
it('Test whenPOST with function as response', function() {
backend.whenPOST(/result/).respond(function(method, url, data) {
return [200, data];
});
browser.get('http://127.0.0.1:8080');
element(by.css('#buttonPOST')).click();
var result = element(by.binding('result'));
expect(result.getText()).toEqual('postedData');
});
});
Advanced Usage
Workflow
HttpBackend workflow is quite simple:
- On browser.get()` a mock module is injected to your angularjs application
- On
when*
or when you call manuallybackend.sync()
, fixtures is synchronised with your angularjs app.
Increase perfomance
For perfomance issue you can disable auto sync:
var backend = new HttpBackend(brower, {autoSync: false});
//Then you should manually call sync function
backend.whenGET(/results/).respond('raoul');
backend.whenGET(/responses/).respond('raoul');
backend.sync();
Httpbackend Methods
when GET, POST, HEAD, PUT, JSONP
add a fixtures, accept literal object, or a callbacksync
, manualy sync fixturesclear
, clear http backend modulereset
, reset all fixture
Development and test
Init project
bower install
npm install
Update Webdriver (used by Grunt)
./node_modules/.bin/webdriver-manager update
Launch test
npm test
Licence
MIT