Forum: Ruby on Rails [Testing] Counting Records in Array from Query

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C4edcb52652d2b17fe2b38ef13bae729?d=identicon&s=25 David Andrew Thompson (Guest)
on 2007-07-11 03:49
(Received via mailing list)
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
         :conditions => "created_at <= now()",
         :limit => 5,
         :order => "created_at desc")

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
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

Any clues. Thanks!
David Andrew Thompson
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-11 03:52
(Received via mailing list)
On 7/11/07, David Andrew Thompson <> wrote:
>          :conditions => "created_at <= now()",
> --
> David Andrew Thompson

If you want to assert that it has 5 exactly, use the asset_equal
on the array's size.

def test_find_recent_widgets
  assert_equal 5, widget.recent_widgets.size

This topic is locked and can not be replied to.