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.

296 CHAPTER 11 Tagging<br />

Because the user that is set up in spec/controllers/comments_controller_spec.rb<br />

doesn’t have permission to tag, when you re-run your spec it will now pass:<br />

2 examples, 0 failures<br />

Good! You have something in place to block users from tagging tickets when they create<br />

a comment. Now you’re only missing the blocking code for tagging a ticket when it<br />

is being created. You can create a spec test for this too, this time in spec/controllers/<br />

tickets_controller_spec.rb. Underneath the “Cannot delete a ticket without permission”<br />

example, add this example:<br />

it "can create tickets, but not tag them" do<br />

Permission.create(:user => user, :thing => project,<br />

➥:action => "create tickets")<br />

post :create, :ticket => { :title => "New ticket!",<br />

:description => "Brand spankin' new" },<br />

:project_id => project.id,<br />

:tags => "these are tags"<br />

Ticket.last.tags.should be_empty<br />

end<br />

You can run this spec by running bin/rspec spec/controllers/tickets_controller<br />

_spec.rb:59, and you’ll see that it fails:<br />

Failure/Error: Ticket.last.tags.should be_empty<br />

expected empty? to return true, got false<br />

Because there is no restriction on tagging a ticket through the create action, there<br />

are tags for the ticket that was just created, and so your example fails. For your<br />

TicketsController’s create action, you can do exactly what you did in the<br />

CommentsController’s create action and change the line that calls tag! to this:<br />

if can?(:tag, @project) || current_user.admin?<br />

@ticket.tag!(params[:tags])<br />

end<br />

When you re-run your spec it will pass:<br />

1 example, 0 failures<br />

Great, now you’re protecting both the ways a ticket can be tagged. Because of this<br />

new restriction, the two scenarios that you created earlier to test this behavior will be<br />

broken.<br />

11.3.2 Tags are allowed, for some<br />

When you run rake cucumber:ok you see them listed as the only two failures:<br />

Failing Scenarios:<br />

cucumber features/creating_comments.feature:45<br />

cucumber features/creating_tickets.feature:48<br />

To fix these two failing scenarios, you use a new step, which you first put in the “Creating<br />

comments” feature underneath this line in the Background for this feature<br />

And "user@ticketee.com" can view the "Ticketee" project

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

Saved successfully!

Ooh no, something went wrong!