将黄瓜翻译成rspec

时间:2012-05-01 21:25:41

标签: ruby-on-rails testing rspec cucumber

如何在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"

2 个答案:

答案 0 :(得分: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提供)打开电子邮件,并且该gem还提供了click_first_link_in_email方法来执行这两个步骤。

然后将您带到一个页面,您可以在其中看到所需的两条消息。

答案 1 :(得分:1)

尝试turnip gem。

  

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