在量角器测试中修改 http 响应 [英] Modify an http response in a protractor test

查看:68
本文介绍了在量角器测试中修改 http 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我们的应用程序的登录过程编写一些端到端的测试,但是我无法找到设置用户需要更改密码的场景的最佳方法.

I'm trying to write some end to end tests for our application's login process, but am having trouble getting my head around the best way to set up the scenario where the user needs to change his password.

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

When our server responds to a successful login, a user object is returned with a changePassword field. The client then inspects the response and redirects accordingly.

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

My problem is getting the test set up so that the changePassword field is set - what is the best approach to use?

我认为我的选择是:

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

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

This seems like the most end to end approach, but is probably also the most effort & code.

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

Somehow intercept the http response in the test and modify the changePassword flag to be set for this test only.

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

Mock the http response completely. Using this approach is the most removed from an end to end test, but is perhaps the simplest?

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

Which is the best or most common approach? Also any general pointers on how to actually implement the above (particularly 1 and 2) with protractor would be great - I'm finding it hard to conceptually get straight in my head, and hence hard to know what to search for.

我使用 量角器 作为测试框架,angular.js 为客户端提供支持,节点 服务器使用 (除其他外)express.jsmongoDB.

I'm using protractor as the test framework, with angular.js powering the client side, and a node server running utilising (among other things) express.js and mongoDB.

推荐答案

进一步考虑这个问题后,选项 1 是最好的解决方案,但并不总是可行的.

Having thought about this further, option 1 is the best solution, but is not always possible.

选项 2 也是可能的,应该避免选项 3.

Option 2 is also possible, and option 3 should be avoided.

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

For option two, a mock module can be created like so: (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 函数中,否则只需在测试中需要时调用即可.

If you want to do this globally, you can put this in the onPrepare function in your protractor file, otherwise just call it when needed in tests.

这篇关于在量角器测试中修改 http 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆