如何使用 Cucumber 来测试 Devise 的 Rememberable 功能?

发布于 2024-09-14 01:30:22 字数 102 浏览 1 评论 0原文

我想要一个 Cucumber 功能来测试 devise(记住我 cookie)的可记住功能。

使用水豚检查“记住我”复选框很容易,但是我应该如何模拟用户在关闭窗口后返回网站?

I'd like to have a Cucumber feature testing the rememberable functionality of devise (a remember me cookie).

It is easy to check the remember me check box using capybara but how should I simulate a user returning to the site after closing their window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦屿孤独相伴 2024-09-21 01:30:22

我想出了以下机架测试 hack 和稍微干净的 selenium api 使用,来测试 Cucumber/capybara 中的 Devise 记住我功能。它只是告诉驱动程序手动删除会话 cookie。并非所有驱动程序都受支持,我只实现了我使用过的两个:

http://gist.github.com/ 484787

这假定会话的 cookie 存储。从场景中删除 @announce 标签以消除冗长。

另一种选择是 Matt Wynne 在邮件列表讨论中建议的,可能正在查看其他 cookie 存储,并通过查询或文件删除来删除它们:

从敏捷 Rails 书籍中删除:

config.action_controller.session_store = CGI::Session::PStore (or just :p_store)
config.action_controller.session_options[:tmpdir] = "/Users/dave/tmp" 
config.action_controller.session_options[:prefix] = "myapp_session_"

或者

rake db:sessions:create
config.action_controller.session_store = :active_record_store

Rails 也有一个重置会话方法,但我相信我们无权访问此方法,因为我们无法挂钩使用水豚测试时进入 Rails 会话。

希望这有帮助,

尼克

I came up with the following rack-test hack, and slightly cleaner selenium api use, to test Devise remember-me functionality in cucumber/capybara. It just tells the driver to manually erase the session cookie. Not all drivers are supported, I only implemented the two I've used:

http://gist.github.com/484787

This assumes cookie storage of the session. Remove @announce tag from the scenario to get rid of the verbosity.

Another option, suggested by Matt Wynne in the mailing list discussion, may be looking at other cookie stores, and deleting them by query or file deletion:

lifted from agile rails book:

config.action_controller.session_store = CGI::Session::PStore (or just :p_store)
config.action_controller.session_options[:tmpdir] = "/Users/dave/tmp" 
config.action_controller.session_options[:prefix] = "myapp_session_"

or

rake db:sessions:create
config.action_controller.session_store = :active_record_store

Rails also has a reset session method, but I believe we don't have access to this because we can't hook into the rails session when testing with capybara.

Hope this helps,

Nick

暖风昔人 2024-09-21 01:30:22

nruth 的要点确实很有帮助,但我觉得按名称删除 cookie 是作弊。我创建了一个步骤,删除浏览器关闭并重新启动时将删除的 cookie(任何没有设置到期日期并在将来设置的 cookie)。

您可以在此提交中看到它(尽管我只为 RackTest 驱动程序做过因为我没有 Selenium 设置)。您还可以在此提交中查看我的login/remember_me功能。我将这些类重构为此提交中的单独文件。

我希望这有帮助。

nruth's gist was really helpful but I felt like deleting the cookie by name was cheating. I created a step that deletes the cookies a browser would delete when it was closed and restarted (any cookie without an expiry date set and set in the future).

You can see it in this commit (though I've only done it for the RackTest driver as I don't have Selenium setup). You can also see my login/remember_me feature in this commit. And I refactored the classes to separate files in this commit.

I hope that's helpful.

送君千里 2024-09-21 01:30:22

我使用 email-spec 来完成此任务。我的场景如下所示:

@allow-rescue
Scenario: Create New Account (Everything cool)
Given I am not authenticated
When I go to register
And I fill in "Name" with "bill"
And I fill in "Email" with "[email protected]"
And I fill in "Password" with "please"
And I fill in "Password Confirmation" with "please"
And I press "Sign up"
Then "[email protected]" should receive an email
And I open the email
And I should see "Confirm my account" in the email body
When I follow "Confirm my account" in the email
Then I should see "Your account was successfully confirmed. You are now signed in."

请注意场景上方的 @allow-rescue 装饰,这是使用 Devise 时必需的。

希望这有帮助。

I used email-spec to accomplish this. My Scenario looks like the following:

@allow-rescue
Scenario: Create New Account (Everything cool)
Given I am not authenticated
When I go to register
And I fill in "Name" with "bill"
And I fill in "Email" with "[email protected]"
And I fill in "Password" with "please"
And I fill in "Password Confirmation" with "please"
And I press "Sign up"
Then "[email protected]" should receive an email
And I open the email
And I should see "Confirm my account" in the email body
When I follow "Confirm my account" in the email
Then I should see "Your account was successfully confirmed. You are now signed in."

Note the @allow-rescue decoration above the scenario that is necessary when using Devise.

Hope this helps.

能否归途做我良人 2024-09-21 01:30:22

我想您可以使用水豚注销,然后重新登录,例如

Given I am on the login screen
And I select 'Remember Me'
And I click 'login'
Then I should be 'logged in'
When I click 'log out'
Then I should be 'logged out' #=> potentially destroy a session here?
When I click log in
Then I should be logged in
And I should not be directed to the login form.

This should use cabybara's current cookie state to model this flow。

I guess you could log out with capybara, and then log back in something like

Given I am on the login screen
And I select 'Remember Me'
And I click 'login'
Then I should be 'logged in'
When I click 'log out'
Then I should be 'logged out' #=> potentially destroy a session here?
When I click log in
Then I should be logged in
And I should not be directed to the login form.

This should use cabybara's current cookie state to model this flow.

不离久伴 2024-09-21 01:30:22

您可以使用 show_me_the_cookies 来实现此目的,如下所示:

And(/^I leave the site$/) do
  expire_cookies
end

You can use show_me_the_cookies for this as shown below:

And(/^I leave the site$/) do
  expire_cookies
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文