27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Linking tickets to users<br />

attributes = attributes.merge!(:user => User.find_by_email!(email))<br />

@project.tickets.create!(attributes)<br />

end<br />

end<br />

With this step definition in place, the feature should pass. Let’s do another run of<br />

bin/cucumber features/viewing_tickets.feature:<br />

1 scenario (1 passed)<br />

19 steps (20 passed)<br />

Let’s now fix up the other two, beginning with the Editing Tickets feature.<br />

6.5.4 Fixing the Editing Tickets feature<br />

You can re-use the step definition you created in the previous section in the features/<br />

editing_tickets.feature by changing the first few lines of the Background to be identical<br />

to the next listing.<br />

Listing 6.11 features/editing_tickets.feature<br />

Given there are the following users:<br />

| email | password |<br />

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

Given there is a project called "TextMate 2"<br />

And "user@ticketee.com" has created a ticket for this project:<br />

| title | description |<br />

| Make it shiny! | Gradients! Starbursts! Oh my! |<br />

When you run the feature—unlike the Viewing Tickets feature—it doesn’t pass, complaining<br />

that it can’t find the field called Title. Uh oh:<br />

cannot fill in, no text field, text area or password field with id,<br />

name, or label 'Title' found (Capybara::ElementNotFound)<br />

Back in the TicketsController, you restricted some of the actions by using the<br />

before_filter:<br />

before_filter :authenticate_user!, :except => [:index, :show]<br />

This before_filter restricts any access to the edit action for people who are not<br />

signed in. In this feature then, you should sign in as the user you create so you can<br />

edit this ticket. Change the first line of the Background to sign in as that user:<br />

Background:<br />

Given there are the following users:<br />

| email | password |<br />

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

And I am signed in as them<br />

When you run this feature, you see the last step in the example is undefined. You must<br />

define this new step so you can sign in as the user set up in the first step of the<br />

Background. Because you assigned @user in the there are the following users step,<br />

you can reference this variable in the new step. Define this new step at the bottom of<br />

features/step_definitions/user_steps.rb by copying the lines from features/signing_in<br />

.feature and doing a couple of replacements, as shown in the following listing.<br />

133

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

Saved successfully!

Ooh no, something went wrong!