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



我正试图为应用程序的登录过程编写一些端到端测试,但我很难找到设置用户需要更改密码的场景的最佳方法。

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

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

我认为我的选择是:

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

    这似乎是最端到端的方法,但可能也是最努力的方法;密码

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

  3. 完全模拟http响应。使用这种方法是端到端测试中最简单的方法,但可能是最简单的吗?

哪种方法是最好的还是最常见的?此外,任何关于如何用量角器实际实现上述内容(特别是12)的通用指针都会很棒——我发现很难在概念上理清思路,因此很难知道该搜索什么。

我使用量角器作为测试框架,angular.js为客户端供电,node服务器使用express.jsmongoDB运行。

经过进一步考虑,选项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')
  )

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

browser.addMockModule('e2eInterceptors', e2eInterceptors)

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

我认为您的第一种方法是最合适的。

无论如何,测试新用户创建是有用的,所以这不是浪费。例如,这个例子似乎是类似的:http://product.moveline.com/testing-angular-apps-end-to-end-with-protractor.html

最新更新