2

I am trying to mock data being responded to from an angularjs service using Protractor. I have been able to successfully do this in other applications by using the following technique.

Creating a mock module that is configurable via protractor.addMockModule

 var httpBackendMock = function() {
    angular.module('mockBackend', ['ngMockE2E', 'MyApp'])
    .value('configData', arguments[0])
    .run(function($httpBackend,configData) {
        console.log('bootstrapped!');
        $httpBackend.whenPOST(/.*/).respond(configData.postHeader, configData.postResponse);
    });
};
module.exports.httpBackendMock = httpBackendMock;

Then creating a protractor test that looks like this

describe('Mocking an httpbackend', function() {

var sampleData = require('./sampleData/blog/normalLanding.json');
var configObject = require('./utils/backendConfig.json');//passed To addMockModule to be accessed via arguments[0]
var MyMockedBackend = require('./utils/httpBackendMock.js');

var ptor = protractor.getInstance();

configObject.postHeader = 200;
configObject.postResponse = sampleData;

 it('Should mock a backnd', function(){
     ptor.addMockModule('mockBackend', MyMockBackend.httpBackendMock, configObject);
     ptor.get('/blog');
    });
});

This worked perfectly for an app that was

  • The only angular application running on the page
  • Was bootstrapped via ng-app

However when working with an app that is manually bootstrapped, the app calls and returns the results from the actual service, rather than the desired mocked response. Also, no errors are thrown. For example's sake, I am trying to respond to all POST requests with the same data.

I have protractors rootElement in my configuration file set to the tag the app bootstraps on.

Does anything look particulary wrong with this settup/approach? Or could it be an issue with the order in which protractor executes in relation to calling addMockModule with a manually bootstrapped application?

Here is a link to a github repo which contains a working example of this with the Angular Phonecat Tutorial from the Angular website. The examples are in test/e2e/utils and scenerios.js

Github example

Cheers!

0 Answers0