# Is mock Object the best way to test scheduling system with a

**URL:** <https://www.ruby-forum.com/t/is-mock-object-the-best-way-to-test-scheduling-system-with-a/106102>\
**Category:** Rails\
**Created:** [June 6, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/is-mock-object-the-best-way-to-test-scheduling-system-with-a/106102 "2007-06-06T13:17:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Yang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@Kevin\_Yang](https://www.ruby-forum.com/u/Kevin_Yang)\
**Post date:** [June 6, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/is-mock-object-the-best-way-to-test-scheduling-system-with-a/106102/1 "2007-06-06T13:17:53Z")

</div>

Hi everyone,  
I hope everyone is doing well.  
I am working on a scheduling system that dealing with Time.now a  
lot. It is getting tricky to write test code for the function.

suppose a function in a model:

def expired?  
self.expire\_time \<= Time.now  
end

i was wondering what is the best way to test it. Or should I say  
that I should design the function in the following way (more  
testable?)

```
def expired?(time)
     self.expire_time <= time
end

then I should use it in the following way : object.expired?

```

(Time.now)  
it is also easier to write test code.

```
Any comments and suggestions will be greatly appreciated :)

```

thanks  
Kevin

---

<div class="post-metadata">

**Author:** ![Kevin\_Yang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@Kevin\_Yang](https://www.ruby-forum.com/u/Kevin_Yang)\
**Post date:** [June 6, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/is-mock-object-the-best-way-to-test-scheduling-system-with-a/106102/2 "2007-06-06T13:28:54Z")

</div>

You could use Mocha ([http://mocha.rubyforge.org](http://mocha.rubyforge.org)) to temporarily stub  
the Time.now method for the duration of an individual test. Something  
like this…

require ‘mocha’

def test\_me  
specific\_time = Time.parse(‘2007-01-01 06:30:00’)  
Time.stubs(:now).returns(specified\_time)

Time.now # =\> Mon Jan 01 06:30:00 +0000 2007  
end

---

<div class="post-metadata">

**Author:** ![Kevin\_Yang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e4f/32.png) [@Kevin\_Yang](https://www.ruby-forum.com/u/Kevin_Yang)\
**Post date:** [June 6, 2007, 1:35pm UTC](https://www.ruby-forum.com/t/is-mock-object-the-best-way-to-test-scheduling-system-with-a/106102/3 "2007-06-06T13:35:56Z")

</div>

Wow~~  
This is genius ~~ how simply the dirty work could be done !

Great thanks to James 🙂
