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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110408/4d61ccaa/attachment.html>
David Chelimsky
2011-Apr-08 12:43 UTC
[rspec-users] Overriding Kernel#inspect just for tests
On Fri, Apr 8, 2011 at 1:52 AM, Stefan Kanev <stefan.kanev at 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 at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ah, that''s clever. Thanks. On Fri, Apr 8, 2011 at 3:43 PM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Fri, Apr 8, 2011 at 1:52 AM, Stefan Kanev <stefan.kanev at 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 at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110408/1b597100/attachment-0001.html>
J. B. Rainsberger
2011-Apr-11 18:06 UTC
[rspec-users] Overriding Kernel#inspect just for tests
On Fri, Apr 8, 2011 at 02:52, Stefan Kanev <stefan.kanev at gmail.com> 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:<snip /> 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.