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.

Locking down states<br />

Then /^I should not see the "([^"]*)" element$/ do |css|<br />

page.should_not(have_css(css),<br />

"Expected to not see the #{css} element, but did.")<br />

end<br />

Here you use Capybara’s have_css matcher to see if the page has an element matching<br />

the CSS selector, which is passed to this step. You also use RSpec’s ability to specify<br />

a custom message if this assertion fails, returning “Expected to not see the<br />

#comment_state_id element, but did” if this element is visible on the page.<br />

If you run this feature using bin/cucumber features/creating_comments<br />

.feature:42, you see that this step is failing because it is seeing the element:<br />

Then I should not see the "#comment_state_id" element<br />

Expected to not see the #comment_state_id element, but did<br />

Right, now that you’ve got your feature to a good-enough point that it ensures that<br />

you shouldn’t see this field, it’s time to hide it and fix the feature. To do this, you use<br />

the can? method to check that the user has permission to change states for this project.<br />

If the user doesn’t have this permission, then you’ll hide the state field. With this<br />

change, the State select box code in app/views/comments/_form.html.erb will now<br />

look like the following listing.<br />

Listing 10.28 app/views/comments/_form.html.erb<br />

<br />

<br />

<br />

@ticket.state_id %><br />

<br />

<br />

Here you use the authorized? method defined in chapter 8 to only display the fields<br />

if the user is authorized to changed the states.<br />

This little change will make your scenario pass because the user you’re signing in<br />

as doesn’t have this particular permission set up. Let’s run it now with bin/cucumber<br />

features/creating_comments.feature:42 to make sure:<br />

1 scenario (1 passed)<br />

10 steps (10 passed)<br />

Good to see, but this view change has definitely broken the scenario in this feature,<br />

which does change the state. You can run this other scenario by running bin/cucumber<br />

features/creating_comments.feature:33; when you do that, you see that it’s<br />

indeed broken:<br />

cannot select option, no select box with id, name, or label 'State' found<br />

You’ve done something similar to this before in chapter 8. Back then, you used a step<br />

that set the current user up with a permission to perform a specific protected action.<br />

This is no different. Let’s write a new step underneath the title of the scenario on line<br />

279

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

Saved successfully!

Ooh no, something went wrong!