angularjsprotractorangular-mock

injecting test mock data into protractor mock module


In my protractor e2e tests, I want to use mock data for httpBackendMock. However I am not able to access/inject the data into mock module. Below is my coffee code.

describe 'my test page', ->
  ptor = protractor.getInstance()
  page = require('./pages/FooPage.js')
  fooPage = new page()
  data = require('./data/testdata.json').data

  describe 'happy path', ->
    beforeEach ->
      httpBackendMock = ->
        angular.module('httpBackendMock', ['ngMockE2E', 'fooApp.controllers']).run ($httpBackend) ->
          mockData = data

Line mockData = data fails (complains data is not defined) when I run the test. I understand this is due to the different context of execution according to https://github.com/angular/protractor/issues/509

So how do I specify the mock data variable in my angular mock module? I do not want to hard code the data in the test.


Solution

  • Found the solution using http-backend-proxy

    describe 'my test page', ->
      page = require('./pages/FooPage.js')
      httpBackend = require('http-backend-proxy')
      fooPage = new page()
      mockData = require('./data/testdata.json')
    
      describe 'my test steps', ->
        beforeEach ->
          proxy = new httpBackend(browser)
          proxy.onLoad.whenGET('/foo/v1/userinfo').respond(200, mockData)
          proxy.onLoad.whenGET(/.*/).passThrough()
          fooPage.get()
          browser.driver.manage().window().maximize()
    
        describe 'main page scenarios', ->
          it 'scenario 1', ->
            usual expect and toBe stuff