# Changing the system date for functional tests

**URL:** <https://www.ruby-forum.com/t/changing-the-system-date-for-functional-tests/80889>\
**Category:** Rails\
**Created:** [January 1, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/changing-the-system-date-for-functional-tests/80889 "2007-01-01T15:31:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [January 1, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/changing-the-system-date-for-functional-tests/80889/1 "2007-01-01T15:31:20Z")

</div>

Is there a way to simulate the functional test being called on  
different days? In other words, the functional test may yield a  
different result when run on different dates. I’d like to set the  
system date in the test to see how the controller responds on different  
dates.

Thanks!  
Tom

---

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [January 1, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/changing-the-system-date-for-functional-tests/80889/2 "2007-01-01T16:39:26Z")

</div>

Tom wrote:

> Is there a way to simulate the functional test being called on  
> different days? In other words, the functional test may yield a  
> different result when run on different dates. I’d like to set the  
> system date in the test to see how the controller responds on different  
> dates.
> 
> Thanks!  
> Tom

You could get the system date from an object, and then use Mocks to  
control what the object sends out.

---

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [January 1, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/changing-the-system-date-for-functional-tests/80889/3 "2007-01-01T18:51:43Z")

</div>

Tom wrote:

> Is there a way to simulate the functional test being called on  
> different days?

You can use a mocking/stubbing library to stub out Time.now in your  
test setup.

Using FlexMock:  
flexstub(Time).should\_recieve(:now).and\_return(Time.mktime(2007,1,1,12,49,00))

Another good library is Mocha.

Dan M.

---

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [January 2, 2007, 5:02am UTC](https://www.ruby-forum.com/t/changing-the-system-date-for-functional-tests/80889/4 "2007-01-02T05:02:53Z")

</div>

Dan M. wrote:

> def my\_day\_of\_the\_week\_based\_method(the\_time = Time.now)
> 
> # do whatever with the\_time
> 
> end

Don’t forget also adding to your /test/fixtures/model.yml files lines  
like  
\<% 3.seconds.ago.to\_s :db %\>. Then your production code can rely on the  
current time without passing it in. Your test fixture data will always  
have  
the correct values relative to the time of the test run.

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [January 2, 2007, 3:44am UTC](https://www.ruby-forum.com/t/changing-the-system-date-for-functional-tests/80889/5 "2007-01-02T03:44:47Z")

</div>

Dan M. wrote:

> You can use a mocking/stubbing library to stub out Time.now in your  
> test setup.

Now that I think about it, this approach really is not good. Most  
time-sensitive logic should be in your models. In all the methods you  
write which use Time.now, you should pass it in to the method like  
this:

def my\_day\_of\_the\_week\_based\_method(the\_time = Time.now)

# do whatever with the\_time

end

Then for your unit tests, you can test it with different times easily.

In your functional tests which use this method in the model, you can  
stub out the model method instead of stubbing out Time.now.

Either approach should work, but having the default Time.now parameter  
in your model methods is a good practice.

Dan M.
