本文介绍了测试保持登录 Rspec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何(以及在​​哪里)为您到处看到的保持登录"功能编写一些 rspec 测试感到有些困惑,包括在 google 登录时.

I'm a bit stumped on how (and where) to write some rspec tests for the "stay signed in" functionality you see all over, including on the google login.

我在网上找到的例子并没有多大帮助.具体来说,我想测试这两个场景.

The examples I found on the web weren't much help. Specifically I want to test these two scenarios.

1

  • a) 用户使用有效凭据登录,而无需单击停留已登录"
  • b) 用户关闭浏览器,重新打开并请求保护页面.
  • 用户不应看到受保护的页面.
  • 用户应该会看到要求他们登录的页面.

2

  • a) 用户使用有效凭据登录并单击停留已登录"
  • b) 用户关闭浏览器,重新打开并请求受保护的页面.
  • 用户不应看到要求他们登录的页面.
  • 用户应该被带到受保护的页面.

我第一次尝试通过删除我存储在会话中的 user_id 来解决模拟浏览器关闭的问题(因为它在浏览器关闭时被删除).但是,这些测试失败了,因为我在请求规范文件夹中工作并且无法访问会话变量.我之前的相关问题:会话在某些 rspec 文件中可用,而在其他文件中不可用.怎么来的?

My first attempt at solving the problem involved simulating a browser close by deleting the user_id I stored in the session (since it gets deleted on browser close). However, these tests failed because I was working in the request spec folder and have no access there to the session variables. My earlier related question: session available in some rspec files and not others. how come?

使用 rspec 进行这些测试的最佳方法是什么?

What is the best way to do these test with rspec?

推荐答案

我认为您应该尝试使用标准的 Rails 方法进行集成测试 - open_session.
就我个人而言,我从来没有这样做过,也不能给你测试过的代码.

I think you should try standard rails method for integration tests - open_session.
Personally I never did that and can't give you tested code.

查看 rails 指南上的多个会话示例.

See multiple sessions example on rails guides.

这篇关于测试保持登录 Rspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:39