1

如何在 rspec 中编写以下功能?

特点:登录;为了以用户身份使用该网站,我希望能够登录

  Scenario: Signing in via confirmation
    Given there are the following users:
      |email            |password|
      |user@example.com |password|
    And "user@example.com" opens the mail with subject
      "Confirmation instructions"
    And they click the first link in the email
    Then I should see "Your account was successfully confirmed"
    And I should see "Signed in as user@example.com"
4

2 回答 2

2

这看起来像是Rails 3 in Action第一版中的功能,我目前正在将其重新编写到第二版中。第二版的特点是这样的:

feature 'Signing in' do
  before do
    Factory(:user, :email => "ticketee@example.com")
  end

  scenario 'Signing in via confirmation' do
    open_email "ticketee@example.com", :with_subject => /Confirmation/
    click_first_link_in_email
    page.should have_content("Your account was successfully confirmed")
    page.should have_content("Signed in as ticketee@example.com")
  end
end

这是使用 Capybara 的新功能语法,其所有意图和目的都与 RSpec 的context块相同。通过使用before,您可以设置一个可以在此功能中使用的用户。在场景中,您使用该open_email方法(由 email_spec gem 提供)打开电子邮件,并且该click_first_link_in_emailgem 提供的方法也执行这两个步骤。

然后将您带到一个页面,您应该能够根据需要查看这两条消息。

于 2012-05-02T12:49:06.977 回答
1

试试萝卜宝石。

Turnip 是 RSpec 的 Gherkin 扩展。它允许您在 Gherkin 中编写测试并在您的 RSpec 环境中运行它们。基本上你可以在 RSpec 中编写黄瓜特性。

于 2012-05-01T22:24:53.963 回答