将测试模拟数据注入量角器模拟模块

时间:2014-08-04 13:48:58

标签: angularjs protractor angular-mock

在我的量角器e2e测试中,我想使用httpBackendMock的模拟数据。但是我无法访问/将数据注入模拟模块。下面是我的咖啡代码。

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
当我运行测试时,

mockData = data失败(抱怨data未定义)。 据我所知,这是由于执行的不同背景所致 https://github.com/angular/protractor/issues/509

那么我如何在angular mock模块中指定模拟数据变量?我不想在测试中对数据进行硬编码。

1 个答案:

答案 0 :(得分:3)

使用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