# \[Testing\] Counting Records in Array from Query

**URL:** <https://www.ruby-forum.com/t/testing-counting-records-in-array-from-query/109060>\
**Category:** Rails\
**Created:** [July 11, 2007, 3:49am UTC](https://www.ruby-forum.com/t/testing-counting-records-in-array-from-query/109060 "2007-07-11T03:49:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [July 11, 2007, 3:49am UTC](https://www.ruby-forum.com/t/testing-counting-records-in-array-from-query/109060/1 "2007-07-11T03:49:38Z")

</div>

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 T.  
[http://www.davidandrewthompson.com](http://www.davidandrewthompson.com)

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [July 11, 2007, 3:52am UTC](https://www.ruby-forum.com/t/testing-counting-records-in-array-from-query/109060/2 "2007-07-11T03:52:38Z")

</div>

On 7/11/07, David Andrew T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> :conditions => "created_at <= now()",
> 
> ```
> 
> –  
> David Andrew T.  
> [http://www.davidandrewthompson.com](http://www.davidandrewthompson.com)

If 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
