【问题标题】:How can I use Cucumber to test Devise's Rememberable functionality?如何使用 Cucumber 测试 Devise 的 Rememberable 功能?
【发布时间】:2010-08-12 13:10:02
【问题描述】:

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

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

【问题讨论】:

  • 除了测试之外,你为什么要设计自己做这个?
  • 因为我认为确保在集成级别测试此功能很重要——也就是说——我是否正确使用它?它是否正常工作?我的客户能看到吗?它在基于黄瓜的验收测试中工作正常吗?。

标签: ruby-on-rails cucumber devise


【解决方案1】:

我想出了以下机架测试技巧,并使用稍微干净的 selenium api 来测试黄瓜/水豚中的 Devise remember-me 功能。它只是告诉驱动程序手动删除会话 cookie。不是所有的驱动都支持,我只实现了我用过的两个:

http://gist.github.com/484787

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

Matt Wynne 在the mailing list discussion 中建议的另一个选项可能是查看其他 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 会话。

希望这会有所帮助,

尼克

【讨论】:

    【解决方案2】:

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

    您可以在this commit 中看到它(尽管我只为 RackTest 驱动程序完成了它,因为我没有 Selenium 设置)。您还可以在this commit 中查看我的登录/remember_me 功能。我重构了这些类以分隔 this commit 中的文件。

    我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      我使用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 "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."
      

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

      希望这会有所帮助。

      【讨论】:

      • 这是很方便的东西,尤其是电子邮件规范,但是我想知道如何测试 Devise 的可记忆功能——即 cookie 存储能力,因此用户无需在每次访问网站时都不断登录。
      【解决方案4】:

      我想你可以用 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 状态来建模这个流程。

      【讨论】:

        【解决方案5】:

        您可以使用show_me_the_cookies,如下所示:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-05
          • 1970-01-01
          • 1970-01-01
          • 2011-03-12
          • 2021-09-24
          • 2011-08-02
          • 1970-01-01
          相关资源
          最近更新 更多