[cm. edit below] We use a shell script to periodically capture curl requests from our test server. These responses are then returned via $ httpBackend.whenGet (...). Response () to intercept and return this data.
So in our index.html
if (document.location.hash === '#test') { addScript('/test/lib/angular-mocks.js'); addScript('/test/e2e/ourTest.js'); window.fixtures = {}; addScript('/test/fixtures/tasks/tasks_p1.js'); // the tasks_p1.js is generated and has "window.fixtures.tasks_p1 = ...json..." addScript('/test/fixtures/tasks/tasks_p2.js'); // the tasks_p2.js is generated and has "window.fixtures.tasks_p2 = ...json..." addScript('/test/e2e/tasks/taskMocks.js\'><\/script>'); }
ourTest.js
angular.module('ourTestApp', ['ngMockE2E','taskMocks']). run(function ($httpBackend, taskMocks) { $httpBackend.whenGET(/views\/.*/).passThrough(); $httpBackend.whenGET(/\/fixtures.*\//).passThrough(); taskMocks.register(); $httpBackend.whenGET(/.*/).passThrough(); });
taskMocks.js
angular.module('taskMocks', ['ngMockE2E']). factory('taskMocks', ['$httpBackend', function($httpBackend) { 'use strict'; return { register: function() { $httpBackend.whenGET(..regex_for_url_for_page1..).respond(window.fixtures.tasks_p1); $httpBackend.whenGET(..regex_for_url_for_page2..).respond(window.fixtures.tasks_p2); } }; }]);
In some cases, our data refers to the current date; for example, "tasks this week," so we mass the captured data in the register () method.
EDIT We are now using Rosie to create factories for our mocking facilities. Thus, there is no longer a CURLing test server for expected json responses. index.html no longer loads these ".js" lights, and the layouts look like this:
taskMocks.js
angular.module('taskMocks', ['ngMockE2E']). factory('taskMocks', ['$httpBackend', function($httpBackend) { 'use strict'; var tasks_p1 = [ Factory.build('task'), Factory.build('task')], tasks_p2 = [ Factory.build('task'), Factory.build('task')] return { register: function() { $httpBackend.whenGET(..regex_for_url_for_page1..).respond(tasks_p1); $httpBackend.whenGET(..regex_for_url_for_page2..).respond(tasks_p2); } }; }]);
source share