我想要一个 Cucumber 功能来测试设计的可记忆功能(一个记住我的 cookie)。
使用 capybara 很容易检查记住我复选框,但我应该如何模拟用户在关闭窗口后返回站点?
我想要一个 Cucumber 功能来测试设计的可记忆功能(一个记住我的 cookie)。
使用 capybara 很容易检查记住我复选框,但我应该如何模拟用户在关闭窗口后返回站点?
我想出了以下机架测试 hack,并使用稍微干净的 selenium api 来测试黄瓜/水豚中的设计记住我的功能。它只是告诉驱动程序手动删除会话 cookie。并非所有驱动程序都受支持,我只实现了我使用过的两个:
这假定会话的 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 也有一个重置会话方法,但我相信我们无法访问它,因为在使用 capybara 进行测试时我们无法挂钩到 rails 会话。
希望这可以帮助,
缺口
我使用电子邮件规范来实现这一点。我的场景如下所示:
@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 "bill@example.com"
And I fill in "Password" with "please"
And I fill in "Password Confirmation" with "please"
And I press "Sign up"
Then "bill@example.com" 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."
请注意使用 Devise 时所需的场景上方的@allow-rescue装饰。
希望这可以帮助。
我想您可以使用 capybara 注销,然后重新登录,例如
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.
这应该使用 cabybara 的当前 cookie 状态来建模此流程。
您可以为此使用show_me_the_cookies,如下所示:
And(/^I leave the site$/) do
expire_cookies
end