advertisement

Print

Cookin' with Ruby on Rails - Integration Tests
Pages: 1, 2, 3, 4, 5, 6, 7

And when we run it...

ruby cookbook2_integration_test.rb

we get...

a successful test run
Figure 10

Paul: Correct me if I'm wrong, CB, but it seems to me like this test would pass if these links were anywhere on the page. What if we wanted them to show up in more than one place? Could we test for that? What if we had some of the same links on a left nav bar, for example?

CB: That's a good question, Paul. And you're right on both counts. The way it stands now, the test would pass if the framework found the links anywhere on the page. So, if the footer links were there, but the ones on the left nav were missing, we wouldn't find that out by running this test. In fact, this is a real good example of how our tests, and the act of creating them first, can guide the development of the app.

The assert_select methods can be nested to constrain the scope of the examination the test framework conducts. Let's make it look for these links inside a set of <div> tags that have an id of "footer." Let's write the test first. I'll wrap our existing assertions in a new one to constrain the search....

def check_the_home_page_footer
  assert_select "div#footer" do
   assert_select "a", {:text=>"Create new recipe", :href=>"recipe/new"}
   assert_select "a", {:text=>"Show all recipes", :href=>"recipe/list"}
   assert_select "a", {:text=>"Show all categories", :href=>"category/list"}
  end
end

So, now the test will only pass if the links we're looking for are inside a <div> with an id="footer". And now I'll rerun the test case.

we have a failing test!
Figure 11

And now that we have a failing test, let's write some code! I'll open the application layout file, since that's where we put the footer code so it would be used for all our app's pages.

application layout prior to change
Figure 12

And now I'll just change the paragraph tags to div tags, giving the opening tag the footer id. So, now we have...

application layout with footers identified
Figure 13

And now when I rerun our test case, we get confirmation that our change was successful, and that we didn't unintentionally break something else in our app; at least nothing that we've got tests written for.

post-change test run
Figure 14

Boss: It seems to me we ought to have more "beef" in the content test, CB. We're checking to see that there's a table on the page and that's good. But I don't see anything that makes sure that the right headings are there, or that the contents have the links they need to have.

CB: Good point, Boss. We can definitely do that. But the real point, from my perspective, is that you could see that our tests need to be beefed up.

Boss: You know... you're right, CB. I didn't have a lot of confidence that the folks from the business would be able to participate the way you said they would, but these tests are really easy to understand.

CB: Excellent! So now let's add the test code to make sure our table has the right contents.

def check_the_home_page_content
  assert_select "table" do
   assert_select "tr" do
     assert_select "th", {:text=>"Recipe"}
     assert_select "th", {:text=>"Category"}
     assert_select "th", {:text=>"Date"}
   end
   assert_select "tr" do
     assert_select "td" do
     assert_select "a", {:text=>"pizza", :href=>"/recipe/show/1"}
     assert_select "a", {:text=>"(delete)", :href=>"/recipe/destroy/1"}
     end
     assert_select "td" do
     assert_select "a", {:text=>"main course", :href=>"/recipe/list?category_id=1"}
     end
     assert_select "td", {:text=>"2007-05-09"}
   end
  assert_select "tr" do
     assert_select "td" do
     assert_select "a", {:text=>"iced tea", :href=>"/recipe/show/2"}
     assert_select "a", {:text=>"(delete)", :href=>"/recipe/destroy/2"}
    end
    assert_select "td" do
     assert_select "a", {:text=>"beverages", :href=>"/recipe/list?category_id=2"}
    end
    assert_select "td", {:text=>"2007-05-09"}
   end
  end
end

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow