On Fri, Apr 8, 2011 at 1:52 AM, Stefan Kanev <stefan.ka...@gmail.com> wrote:
> 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?

Of course! Remember, this is just Ruby. Put something like this in
spec/spec_helper.rb:

####################
module InspectModelForRSpec
  def inspect
    ...
   end
end

ActiveRecord::Base.send(:include, InspectModelForRSpec)
####################

Now, as long as the code is not inadvertently loading up
spec/spec_helper.rb when running the app or console, you'll only see
the result of this when you run specs.

HTH,
David

>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
>
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to