如何在 rspec 中编写以下功能?
Feature: Signing in; In order to use the site as a user, I want to be able to sign in
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"
最佳答案
这看起来像是 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
block 相同。通过使用 before
,您可以设置一个可以在此功能中使用的用户。在该场景中,您使用 open_email
方法(由 email_spec gem 提供)打开电子邮件,并使用该 gem 提供的 click_first_link_in_email
方法来执行这两个步骤。
然后会将您带到一个页面,您应该能够在其中看到所需的两条消息。
关于ruby-on-rails - 将 cucumber 翻译成 rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10404594/