David Andrew Thompson
2007-Jul-11 01:48 UTC
[Testing] Counting Records in Array from Query
I can''t seem to figure out how to assert in my test that my method which finds 5 records is in fact returning five records. In my model: # app/models/widget.rb ... def self.recent_widgets find(:all, :conditions => "created_at <= now()", :limit => 5, :order => "created_at desc") end I created a widgets.yml fixture with five records in it. In my test_widget.rb unit test I want to assert that there are in fact five records being returned with the recent_widgets method I''ve gone as far as: # tests/unit/test_widget.rb ... fixtures :widgets def test_find_recent_widgets w = widget.recent_widgets assert_not_nil w end Any clues. Thanks! -- David Andrew Thompson http://www.davidandrewthompson.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 7/11/07, David Andrew Thompson <dandrew.thompson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I can''t seem to figure out how to assert in my test that my method which > finds 5 records is in fact returning five records. > > In my model: > > # app/models/widget.rb > ... > def self.recent_widgets > find(:all, > :conditions => "created_at <= now()", > :limit => 5, > :order => "created_at desc") > end > > I created a widgets.yml fixture with five records in it. > > In my test_widget.rb unit test I want to assert that there are in fact > five records being returned with the recent_widgets method > > I''ve gone as far as: > > # tests/unit/test_widget.rb > ... > fixtures :widgets > > def test_find_recent_widgets > w = widget.recent_widgets > assert_not_nil w > end > > Any clues. Thanks! > -- > David Andrew Thompson > http://www.davidandrewthompson.comIf you want to assert that it has 5 exactly, use the asset_equal assertion on the array''s size. def test_find_recent_widgets assert_equal 5, widget.recent_widgets.size end HTH Daniel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---