Overriding Kernel#inspect just for tests

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.

On Fri, Apr 8, 2011 at 1:52 AM, Stefan K. [email protected]
wrote:

Failure/Error: topic.replies_on_page(1).should == [second, first]
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

Ah, that’s clever. Thanks.

On Fri, Apr 8, 2011 at 02:52, Stefan K. [email protected]
wrote:

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:

If you only want to check the identity of the Reply objects, then
shouldn’t you only check the identity of the Reply objects?

topic.replies_on_page(1).collect(&:id).should == [second,
first].collect(&:id)

Now introduce a custom matcher and off you go.

topic.replies_on_page(1).should match_models([second, first]), maybe?

J. B. (Joe) Rainsberger :: http://www.jbrains.ca ::
http://blog.thecodewhisperer.com
Diaspar Software Services :: http://www.diasparsoftware.com
Author, JUnit Recipes
2005 Gordon Pask Award for contribution to Agile practice :: Agile
2010: Learn. Practice. Explore.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs