修改量角器测试中的http响应

Ed *_*ffe 4 testing protractor

我试图写一些端到端的为我们的应用程序的登录流程测试,但我有麻烦我的脑袋周围设置当用户需要更改其密码的情况下的最佳途径.

当我们的服务器响应成功登录时,将返回带有changePassword字段的用户对象.然后,客户端检查响应并相应地重定向.

我的问题是设置测试以便设置changePassword字段 - 使用的最佳方法是什么?

我认为我的选择是:

  1. 为服务器设置测试设置和拆卸脚本,专门为测试运行创建一个全新用户,并changePassword在数据库中设置标志.

    这似乎是最端对端的方法,但也可能是最省力的代码.

  2. 以某种方式拦截测试中的http响应并修改changePassword仅为此测试设置的标志.

  3. 完全模拟http响应.使用这种方法是从端到端测试中删除最多的,但也许是最简单的?

哪种方法最好或最常用?关于如何用量角器实际实现上述(特别是12)的任何一般指示都会很棒 - 我发现在概念上难以直接进入我的脑海,因此很难知道要搜索什么.

我用量角器作为测试框架,angular.js客户端供电,并且节点服务器上运行使用(除其他事项外)express.jsMongoDB的.

Ed *_*ffe 5

进一步考虑后,选项1是最佳解决方案,但并非总是可行.

备选方案2也是可能的,应避免备选方案3.

对于选项二,可以像这样创建一个模拟模块:(coffeescript)

e2eInterceptors =->

  angular.module('e2eInterceptors', [])
  .factory('loginInterceptor', ()->
    response: (response)->
      # Only edit responses we are interested in
      return response unless response.match(/login/)
      # do the modifiations
      response.data.changePassword = true
      # return the response
      return response
  )
  .config(($httpProvider)->
    $httpProvider.interceptors.push('loginInterceptor')
  )
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用将此模块注入测试

browser.addMockModule('e2eInterceptors', e2eInterceptors)
Run Code Online (Sandbox Code Playgroud)

如果要全局执行此操作,可以将其放在onPrepare量角器文件中的函数中,否则只需在测试中需要时调用它.