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.

Viewing tickets<br />

Quite the long feature! We’ll go through it piece by piece in just a moment. First, let’s<br />

examine the within usage in your scenario. Rather than checking the entire page for<br />

content, this step checks the specific element using Cascading Style Sheets (CSS)<br />

selectors. The #ticket prefix finds all elements with an ID of ticket that contain an<br />

h2 element with the content you specified. This content should appear inside the<br />

specified tag only when you’re on the ticket page, so this is a great way to make sure<br />

that you’re on the right page and that the page is displaying relevant information.<br />

The first step passes because you defined it previously; the next one is undefined.<br />

Let’s see this by running bin/cucumber features/viewing_tickets.feature:<br />

Undefined step: "that project has a ticket:" (Cucumber::Undefined)<br />

The bottom of the output tells you how to define the step:<br />

Given /^that project has a ticket:$/ do |table|<br />

# table is a Cucumber::Ast::Table<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

This step in the scenario is defined using the following syntax:<br />

| title | description |<br />

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

For Cucumber, this syntax represents a table, which is what the step definition hints at.<br />

Using the code shown in the following listing, define this step inside a new file at features/step_definitions/ticket_steps.rb.<br />

Listing 5.9 features/step_definitions/ticket_steps.rb<br />

Given /^that project has a ticket:$/ do |table|<br />

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

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

end<br />

end<br />

Because you used a table here, Cucumber provides a hashes method for the table<br />

object, which uses the first row in the table as keys and the rest of the rows (as many as<br />

you need) for the values of hashes stored in an array. In this step, you iterate through<br />

this array, and each hash represents the attributes for the tickets you want to create.<br />

One thing that you haven’t done yet is define the @project variable used inside<br />

this iterator. To do that, open features/step_definitions/project_steps.rb and change<br />

this line<br />

Factory(:project, :name => name)<br />

to the following:<br />

@project = Factory(:project, :name => name)<br />

Instance variables are available throughout the scenario in Cucumber, so if you define<br />

one in one step, you may use it in the following steps. If you run the feature again, you<br />

109

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

Saved successfully!

Ooh no, something went wrong!