0

Please see my code example below, I have a protractor test that get's run as a GULP task by angular-protractor.

I want to be able to mock some REST calls for all it blocks, but also have access to the httpBackend for specific it blocks.

I'm having trouble sharing the $httpBackend variable around though:

  • I can't use this.VARIABLE_NAME because of the scope I am in inside the run function.

Code Example

describe('Train Station Search Component', function() {

    beforeEach(function() {
        //Mock any REST calls here.
        browser.addMockModule('httpBackendMock', function() {
        angular.module('httpBackendMock', ['MyApp', 'ngMockE2E'])
               .run(function($httpBackend) {
                   //Mock call relevant to both it blocks.
                   $httpBackend.whenPOST(/^(.*\/api\/search)/).respond({...});
               });
        });
    );

    it('should check one piece of functionality', function() {
        //Expect call with data relevant only for this it block.
        $httpBackend.expectPOST(...);
    });

    it('should check another piece of functionality', function() {
       //Expect call with data relevant only for this it block.
       $httpBackend.expectPOST(...);
    });

};

Any help appreciated.

Anton Rand
  • 322
  • 5
  • 20

1 Answers1

0

You can use $injector to inject $httpBackEnd service.

var $httpBackend
beforeEach(inject(function($injector) {
// Set up the mock http service responses
$httpBackend = $injector.get('$httpBackend');
});

Go through the link https://docs.angularjs.org/api/ngMock/service/$httpBackend

Abhi
  • 8,935
  • 7
  • 37
  • 60
  • Thank you for relying. I'm not having any luck with that, I have ng-mock.js in the HTML page but I get an error message running the test that inject is undefined. It's an object in the browser window. I could get this using executeScript but it seems a bit hack. – Anton Rand Sep 13 '16 at 18:02