# Mock today

**URL:** <https://www.ruby-forum.com/t/mock-today/153654>\
**Category:** Rails\
**Created:** [November 19, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/mock-today/153654 "2008-11-19T21:21:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [November 19, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/mock-today/153654/1 "2008-11-19T21:21:05Z")

</div>

In some of my unit tests, I find the need to set today to a different  
value than the actual today. For example, to test the model method  
“balance\_of\_today”, which takes no argument, I want to have Date.today  
be a specific date for the purpose of testing. In other words, I want  
to do something like this:

mock\_today(Date.new(2008, 2, 1)) do  
… test code…  
end

My implementation for mock\_today is:

def mock\_today(date)  
$\_mock\_test\_date = date

```
Date.class_eval do
  class <<self
    alias original_today today
  end

  def self.today
    $_mock_test_date
  end
end

yield

Date.class_eval do
  class <<self
    alias today original_today
  end
end

```

end

So my questions are:

1. Is it a good idea to mock Date.today?

2. Can my implementation of mock\_today be improved? I’m particularly  
worried about of the use of global variable, and of class\_eval, since  
I heard class\_eval is becoming outdated as Ruby 1.9 is coming out.

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [November 19, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/mock-today/153654/2 "2008-11-19T21:40:31Z")

</div>

On Nov 19, 8:20 pm, Eric LIn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My implementation for mock\_today is:  
> $\_mock\_test\_date  
> end
> 
> So my questions are:
> 
> 1. Is it a good idea to mock Date.today?
> 
> 2. Can my implementation of mock\_today be improved? I’m particularly  
> worried about of the use of global variable, and of class\_eval, since  
> I heard class\_eval is becoming outdated as Ruby 1.9 is coming out.

I occasionally “timetravel” by overriding Time.now (and Date.today  
calls Time.now eventually).  
It goes a little like this

class Time  
cattr\_accessor :frozen\_now  
self.frozen\_now = nil  
class \<\< self  
def now\_with\_freeze  
if @@frozen\_now  
@@frozen\_now  
else  
now\_without\_freeze  
end  
end  
alias\_method\_chain :now, :freeze  
end  
end

Then you freeze by doing Time.frozen\_now = … and unfreeze by setting  
it back to nil. You could easily refactor this to have a block like  
syntax.

Fred

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [November 19, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/mock-today/153654/3 "2008-11-19T21:44:40Z")

</div>

On Nov 19, 2008, at 3:37 PM, Frederick C. wrote:

> > end
> > 
> > ```
> > end
> > 
> > ```
> > 
> > I heard class\_eval is becoming outdated as Ruby 1.9 is coming out.  
> > if @@frozen\_now  
> > it back to nil. You could easily refactor this to have a block like  
> > syntax.
> 
> Fred

Or use flexmock:

require ‘rubygems’  
gem :flexmock  
require ‘flexmock/test\_unit’

# then in your test

```
 stopped_clock = Date.new(2008, 2, 1)
 flexmock(Date).should_receive(:today).returns(stopped_clock)

```

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [November 19, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/mock-today/153654/4 "2008-11-19T22:00:10Z")

</div>

Thanks, both of these are good suggestions. I will look into them.

On Nov 19, 12:43 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
