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.

438 CHAPTER 16 Basic performance enhancements<br />

Given /^there are (\d+) tickets for this project$/ do |arg1|<br />

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

end<br />

Then /^I should see (\d+) pages of pagination$/ do |arg1|<br />

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

end<br />

Then /^I see page (\d+) of tickets for this project$/ do<br />

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

end<br />

The first step definition here has to do with tickets, and so you’ll put it in features/<br />

step_definitions/ticket_steps.rb using the code shown in the following listing.<br />

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

Given /^there are (\d+) tickets for this project$/ do |number|<br />

number.to_i.times do |i|<br />

@project.tickets.create!(:title => "Test",<br />

:description => "Placeholder ticket.",<br />

:user => @user)<br />

end<br />

end<br />

This small piece of code will create as many tickets as you’ve specified for the<br />

@project object set up by the “Given there is a project” step, using the @user variable<br />

set up by the “Given there are the following users” step. It’s not important what the<br />

title and description attributes are for these tickets, just that you have enough of them<br />

to trigger the pagination links to appear.<br />

The next two undefined steps can go into a new file called features/step<br />

_definitions/pagination_steps.rb, as they’re about the pagination more than any other<br />

resource in your application.<br />

The Kaminari paginate method you’ll use in your view shortly will output a nav<br />

element with the class of pagination. Inside this nav element there are a couple of<br />

other elements. The first is a span element with the class of prev, which would contain<br />

a Previous button if you’re a page or two in with pagination. After that, there are a<br />

couple more span elements, all with the class of page. The span tag representing the<br />

current page has an additional class name of current, which we’ll discuss a little later.<br />

You can count these span.page elements easily, using the css method to find them<br />

using a CSS selector and then using count on what that returns to count the number<br />

of pages shown in pagination.<br />

By gathering up these page elements and counting them, you easily assert that<br />

there’s pagination on this page. You can define the next step like this in features/<br />

step_definitions/pagination_steps.rb:<br />

Then /^I should see (\d+) pages of pagination$/ do |number|<br />

pages = all(".pagination .page")<br />

pages.count.should eql(number.to_i)<br />

end

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

Saved successfully!

Ooh no, something went wrong!