# How do I set up a test without using fixture in this case

**URL:** <https://www.ruby-forum.com/t/how-do-i-set-up-a-test-without-using-fixture-in-this-case/107597>\
**Category:** Rails\
**Created:** [June 23, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/how-do-i-set-up-a-test-without-using-fixture-in-this-case/107597 "2007-06-23T21:25:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [June 23, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/how-do-i-set-up-a-test-without-using-fixture-in-this-case/107597/1 "2007-06-23T21:25:42Z")

</div>

Let’s say I have 3 records in SampleModel:

id: 1  
created\_at: 5/3/2007

id: 2  
created\_at: 6/3/2007

id: 3  
created\_at: 7/3/2007

I have a controller action that should only return records that were  
created after 6/3/2007. I can see how to do it with fixtures, but can I  
perform this type of test with a mock object and/or stubs?

Thanks!  
Nelson

---

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [June 23, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/how-do-i-set-up-a-test-without-using-fixture-in-this-case/107597/2 "2007-06-23T22:24:46Z")

</div>

On 6/23/07, Nelson H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> created\_at: 7/3/2007
> 
> I have a controller action that should only return records that were  
> created after 6/3/2007. I can see how to do it with fixtures, but can I  
> perform this type of test with a mock object and/or stubs?

There’s really two parts to this - how would you do it using mocks,  
and what exactly would you mock? If it were me, it would look like  
this (using rspec, but you can do the same thing w/ test/unit+mocha or  
flexmock):

describe SampleModelsController, “handling GET /date\_filtered\_action” do  
it “should ask SampleModel for items created before the submitted  
date” do  
SampleModel.should\_receive(:find\_created\_before).with(“6/22/2007”)  
get “date\_filtered\_action”, :cutoff =\> “6/22/2007”  
end  
…  
end

class SampleModelsController \< Application  
def date\_filtered\_action  
@sampleModels = SampleModel.find\_created\_before(params[:cutoff])  
…  
end  
end

And then I’d test the find\_created\_before method in my description of  
SampleModel’s behaviour (TestSampleModel if you’re using test/unit).

Now if you want to use mocks in the model test as well, you could do  
this:

describe SampleModel do  
it “should provide items created before a submitted date” do  
SampleModel.should\_receive(:find).with(:conditions =\> ["created\_at

> ?", “6/22/2007”])  
> SampleModel.find\_created\_before(“2007-06-22”)  
> end  
> end

class SampleModel \< ActiveRecord::Base  
def find\_created\_before(date)  
self.find(:all, :conditions =\> [“created\_at \> ?”,  
Date.parse(date).to\_s(:db))]  
end  
end

Note that this sort of testing is very risky if you’re not also doing  
integration testing of some sort. For example, this would pass even if  
there was no created\_at column in the sample\_models table. I happen to  
do a lot of this sort of testing, but I do it in concert with customer  
acceptance tests (i.e. high level end-to-end tests).

Hope that helps.

David

ps - I didn’t run this, so please forgive any errors.
