[rspec-users] exemplary way to show a list is sorted?

Phlip phlip2005 at gmail.com
Sun Feb 8 18:48:41 EST 2009


RSpeckers:

I'm asserting that a list should be sorted by affinity. The following
code is good enough for government work, but is there some more clever
way to show that each element is equal or less than the previous
element?

  it 'should rank posts by affinity' do
    list = @map.sort_frobs_by_affinity

    list[1].affinity.should >= list[1].affinity
    list[2].affinity.should >= list[2].affinity
    list[3].affinity.should >= list[3].affinity
    list[4].affinity.should >= list[4].affinity
  end

Notice I could just write...

  list.map(&:affinity) == list.map(&:affinity)

...but it seems that the point of BDD is to make the business logic
obvious, not obscured in programming details, right?

-- 
 Phlip
 http://www.oreilly.com/catalog/9780596510657/
 ^ assert_xpath
 http://tinyurl.com/yrc77g  <-- assert_latest Model


More information about the rspec-users mailing list