3

[Note: Using Rails 3.1.]

I am trying to test that multiple models are being displayed in the proper order on my page, in my case, by desc date with the most recent on top.

I know I can do the following to check if something exists on the page:

And I should see "Some content on my page."

But I want to do something along the lines of:

And I should see "Most Recent" before "Really old"

How would I go about writing steps to do that? I believe "And I should see" just scans the entire page for the specified argument, just not sure how to approach correct order.

Thanks in advance!

ardavis
  • 9,842
  • 12
  • 58
  • 112

2 Answers2

6

Use String#index on page.body to find the position of each, and assert first < second, e.g.

And /^I should see "([^"]*)" before "([^"]*)"$/ do |phrase_1, phrase_2|
  first_position = page.body.index(phrase_1)
  second_position = page.body.index(phrase_2)
  first_position.should < second_position
end
Andy Waite
  • 10,785
  • 4
  • 33
  • 47
0

A more flat version, with MiniTest assertion:

And /^I should see "([^"]*)" before "([^"]*)"$/ do |phrase_1, phrase_2|
  assert page.body.index(phrase_1) < page.body.index(phrase_2)
end
matiasmasca
  • 605
  • 8
  • 14