Translating cucumber to rspec
How to write the following feature in 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| |firstname.lastname@example.org |password| And "email@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 firstname.lastname@example.org"
This looks like the feature from the first edition of Rails 3 in Action, which I'm currently re-writing into a second edition. The second edition's feature goes like this:
feature 'Signing in' do before do Factory(:user, :email => "email@example.com") end scenario 'Signing in via confirmation' do open_email "firstname.lastname@example.org", :with_subject => /Confirmation/ click_first_link_in_email page.should have_content("Your account was successfully confirmed") page.should have_content("Signed in as email@example.com") end end
This is using Capybara's new feature syntax, which for all intents and purposes is the same as RSpec's context blocks. By using a before you set up a user that you can use inside this feature. Inside the scenario, you use the open_email method (provided by the email_spec gem) to open the email, and the click_first_link_in_email method also provided by that gem to perform those two steps.
That then takes you to a page where you should be able to see the two messages as so desired.
Give turnip gem a try.
Turnip is a Gherkin extension for RSpec. It allows you to write tests in Gherkin and run them through your RSpec environment. Basically you can write cucumber features in RSpec.