Hi all.

Occasionally, I write specs that verify the order in which some ActiveRecord
objects are returned. For example, in a toy project I do:

Reply.stub :per_page => 2

topic.replies_on_page(1).should == [second, first]


The spec out quickly get unwieldy when it fails:

1) Topic paginates its replies in chronological order
   Failure/Error: topic.replies_on_page(1).should == [second, first]
   expected: [#<Reply id: 20, topic_id: 21, user_id: 98, body: "Body",
created_at: "2011-04-07 06:38:34", updated_at: "2011-04-08 06:38:34">,
#<Reply id: 19, topic_id: 21, user_id: 97, body: "Body", created_at:
"2011-04-06 06:38:34", updated_at: "2011-04-08 06:38:34">]
       got: [#<Reply id: 19, topic_id: 21, user_id: 97, body: "Body",
created_at: "2011-04-06 06:38:34", updated_at: "2011-04-08 06:38:34">,
#<Reply id: 20, topic_id: 21, user_id: 98, body: "Body", created_at:
"2011-04-07 06:38:34", updated_at: "2011-04-08 06:38:34">] (using ==)


This is a rather simple example with a simple model. If Topic would have
more columns or the list contains more items, it quickly gets very hard to
understand the failure.

I tried overriding #inpsect in my models, but I'm not sure I like that.
Results are a lot better, but now using rails console gets trickier.

Is there a way to do this just for RSpec? I found an old thread in
rspec-devel [1], but I don't think it ever resulted to something that got
merged.

[1]:
http://old.nabble.com/Better-inspect-method-for-active-record-objects-in-rspec-rails-td20297318.html
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to