27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Form sign-in<br />

When I follow "Sign in"<br />

no link with title, id or text 'Sign in' found (Capybara::ElementNotFound)<br />

You should add the link directly under the Sign Up link in app/views/layouts<br />

/application.html.erb, as shown in the following listing.<br />

Listing 6.7 app/views/layouts/application.html.erb<br />

<br />

When you run the feature again, it still fails, but this time on the final step:<br />

And I press "Sign in"<br />

Then I should see "Signed in successfully."<br />

expected there to be content "Signed in successfully." in "[text]"<br />

It fails because users who have not yet confirmed their account by clicking the link in<br />

the email can’t sign in. To fix this, you could confirm the users with the first step in<br />

this scenario, but it would break the first scenario because it requires users to be<br />

unconfirmed.<br />

To fix this, alter the first scenario in this feature to contain the following as its first<br />

step:<br />

Given there are the following users:<br />

| email | password | unconfirmed |<br />

| user@ticketee.com | password | true |<br />

With this small change, the step now has an additional key available in the attributes<br />

hash in the step definition. If this step is called with an unconfirmed user, it doesn’t<br />

confirm the user; otherwise it does. Let’s alter this step definition in features/<br />

step_definitions/user_steps.rb:<br />

Given /^there are the following users:$/ do |table|<br />

table.hashes.each do |attributes|<br />

unconfirmed = attributes.delete("unconfirmed") == "true"<br />

@user = User.create!(attributes)<br />

@user.confirm! unless unconfirmed<br />

end<br />

end<br />

At the top of the iteration over table.hashes, you now call attributes<br />

.delete("unconfirmed"), which removes the unconfirmed key from the attributes<br />

hash, returning its value in the process. If that value is equal to true then<br />

unconfirmed is also set to true. If that’s the case, the final line in the iterator isn’t<br />

called and the user isn’t confirmed. Otherwise, as in the case in the second scenario of<br />

the feature, the user is confirmed and allowed to sign in.<br />

When you run bin/cucumber features/signing_in.feature again, both scenarios<br />

pass:<br />

2 scenarios (2 passed)<br />

12 steps (12 passed)<br />

127

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!