Angular E2E Check with httpBackend bullying?

Hot discussion between me and my boss about Angular E2E testing. according to vojitajina pull request we need to start the server to run e2e tests. Thus, the launch of the e2e test includes a real server, and on a real server - DB. This makes the test slow. Now the question is how to check e2e without involving a real server? is there any way to use httpBackend to and e2e angular API where i can use browser (), element (), select () for my tests?

+4
source share
2 answers

[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

 /*global angular */ 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

 /*global angular */ 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); } }; }]); 
+5
source

To answer your question, yes, there is a way to do this without involving a real server, and you can use $ httpBackend to mock answers in e2e tests, but it's not as simple as mocking them in unit tests. You also need to include angular -mocks.js in index.html as well as in 'ngMockE2E' in app.js.

How to make fun of an AJAX request?

-1
source

All Articles