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.

274 CHAPTER 10 Tracking state<br />

Listing 10.25 features/managing_states.feature<br />

Feature: Managing states<br />

In order to change information about a state<br />

As an admin<br />

I want to be able to set a state's name and default status<br />

Background:<br />

Given I have run the seed task<br />

And I am signed in as "admin@ticketee.com"<br />

Scenario: Marking a state as default<br />

Given I am on the homepage<br />

When I follow "Admin"<br />

And I follow "States"<br />

And I follow "Make default" for the "New" state<br />

Then I should see "New is now the default state."<br />

In this scenario you’ve got one new step B, which you need to define for this feature<br />

to run. Let’s run this feature now to get the step definitions for these steps by running<br />

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

When /^I follow "([^"]*)" for the "([^"]*)" state$/ do |arg1, arg2|<br />

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

end<br />

You put this step definition inside features/step_definitions/state_steps.rb and work on<br />

getting the first definition to pass for now. This definition is used like this:<br />

And I follow "Make default" for the "New" state<br />

Which is a slightly modified version of the following:<br />

And I follow "Make Default" within "#some_state"<br />

B New step<br />

You’re not using the within variant here directly because you’re not going to be using<br />

a statically set id attribute for the state in the view but rather setting it to something<br />

like state_3 using this code:<br />

<br />

<br />

<br />

(Default)<br />

<br />

<br />

<br />

<br />

But you use this step within your custom step. You’re also not using the step When I<br />

follow "Make Default" because this will follow the first Make Default link on the<br />

page, which may or may not be the one that you want. With all of this in mind, you can<br />

redefine your step as follows:<br />

When /^I follow "([^"]*)" for the "([^"]*)" state$/ do |link, name|<br />

state = State.find_by_name!(name)<br />

steps(%Q{When I follow "#{link}" within "#state_#{state.id}"})<br />

end

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

Saved successfully!

Ooh no, something went wrong!