Http backend mock module for protractor
npm install httpbackendinclude angular mock script https://github.com/angular/bower-angular-mocks
varHttpBackend=require('httpbackend');varbackend=null;describe('Test Http backend methods',function(){beforeEach(function(){backend=newHttpBackend(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');varresult=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();varresult=element(by.binding('result'));expect(result.getText()).toEqual('postedData');});});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.
For perfomance issue you can disable auto sync:
varbackend=newHttpBackend(brower,{autoSync: false});//Then you should manually call sync functionbackend.whenGET(/results/).respond('raoul');backend.whenGET(/responses/).respond('raoul');backend.sync();when GET, POST, HEAD, PUT, JSONPadd a fixtures, accept literal object, or a callbacksync, manualy sync fixturesclear, clear http backend modulereset, reset all fixture
Init project
bower install
npm installLaunch test
npm testMIT