Exploration Through Example

Example-driven development, Agile testing, context-driven testing, Agile programming, Ruby, and other things of interest to Brian Marick
191.8 167.2 186.2 183.6 184.0 183.2 184.6

Sat, 03 May 2003

Changing Time in tests

Someone on the testdrivendevelopment mailing list asked a question that boils down to this. Suppose you have code that looks like this:


  class Mumble
    def do_something(...)
      ...
      ... Time.now ...  # Time.now gives current time.
      ...
    end
  end

You want do_something to work with all kinds of times: times in the past, times in the future, this exact instant, times way in the past, the times that don't exist because of daylight saving time, etc.

The conventional answer is that time should be a variable, not (in effect) a global. Either pass it in to do_something like this:


    def do_something(..., now = Time.now)
      ...
      ... now ...
      ...
    end

Or, if multiple methods depend on time, add a method that changes the object's current time:


  class Mumble
    def set_time(time)
      @now = time
    end

    def now
      @now || Time.now  # iff no time set, use the system time.
    end
  end

Or, if you're squeamish about giving clients the ability to change time, make Mumble have a now method that returns Time.now and make a TestableMumble subclass that looks like the above.

I recently found myself doing something different. In Ruby, all classes are "open". You can add methods to any of them at any time. Rather than adding a time-setting method to Mumble, I added it to Time. It looks like this:

  class << Time                       # change Time's methods
    alias_method :original_now, :now  # save old method.
  
    def set(time)
      @now = time
    end
  
    def advance(seconds)
      @now += seconds
    end
  
    def now
      @now || original_now
    end
  
    def use_system_time
      @now = nil
    end
  end

My tests look like this:


    def test_active_time_extends_over_day
      job 'oldie'
      Time.set(Time.local(2002, 'feb', 28, 14, 30))
      start 'oldie'
      ...
      assert_equal("'oldie', with 24.00 hours from 02002/02/28 2:30 PM.",
                   result)
    end

This is more convenient than running around telling a bunch of objects or methods what time they should be thinking it is. Instead, I tell everything what time it should be thinking it is. It's worked out rather well. I've not encountered a case where, say, I want two different objects in the same test to have different notions of "now".

That's not to say I don't use the more traditional ways for things like telling objects what servers they're talking to. But, in the case of time, there's already a global notion of "now" defined by your language. By introducing object- or method-specific notions of "now", you're violating Occam's Razor (in the "do not multiply entities unnecessarily" sense). What do I mean by that?

Consider servers. You can simply declare there is no global variable that an object can query to find out which server to use. If an object wants to know, it has to be passed the server or an object it can query for the server. You cannot similarly declare that Time does not exist, and you cannot prevent programmers from using it, especially not the programmers of third-party libraries.

It simply seems less error-prone to have a single source of time information that all code must use. Then, for testing, we treat the assembled product or any piece of it as a brain in a vat, unable to tell whether it's interacting with the real world's time or a simulation.

More generally, we need to be able to control all the product's perceptions. For this, it seems we need language/substrate support of the type Ruby allows. I believe this was also once a property of Community.com/Combex's Java Vat (hence the name), but I'm not sure if that's true any more.

## Posted at 10:43 in category /ruby [permalink] [top]

About Brian Marick
I consult mainly on Agile software development, with a special focus on how testing fits in.

Contact me here: marick@exampler.com.

 

Syndication

 

Agile Testing Directions
Introduction
Tests and examples
Technology-facing programmer support
Business-facing team support
Business-facing product critiques
Technology-facing product critiques
Testers on agile projects
Postscript

Permalink to this list

 

Working your way out of the automated GUI testing tarpit
  1. Three ways of writing the same test
  2. A test should deduce its setup path
  3. Convert the suite one failure at a time
  4. You should be able to get to any page in one step
  5. Extract fast tests about single pages
  6. Link checking without clicking on links
  7. Workflow tests remain GUI tests
Permalink to this list

 

Design-Driven Test-Driven Design
Creating a test
Making it (barely) run
Views and presenters appear
Hooking up the real GUI

 

Popular Articles
A roadmap for testing on an agile project: When consulting on testing in Agile projects, I like to call this plan "what I'm biased toward."

Tacit knowledge: Experts often have no theory of their work. They simply perform skillfully.

Process and personality: Every article on methodology implicitly begins "Let's talk about me."

 

Related Weblogs

Wayne Allen
James Bach
Laurent Bossavit
William Caputo
Mike Clark
Rachel Davies
Esther Derby
Michael Feathers
Developer Testing
Chad Fowler
Martin Fowler
Alan Francis
Elisabeth Hendrickson
Grig Gheorghiu
Andy Hunt
Ben Hyde
Ron Jeffries
Jonathan Kohl
Dave Liebreich
Jeff Patton
Bret Pettichord
Hiring Johanna Rothman
Managing Johanna Rothman
Kevin Rutherford
Christian Sepulveda
James Shore
Jeff Sutherland
Pragmatic Dave Thomas
Glenn Vanderburg
Greg Vaughn
Eugene Wallingford
Jim Weirich

 

Where to Find Me


Software Practice Advancement

 

Archives
All of 2006
All of 2005
All of 2004
All of 2003

 

Join!

Agile Alliance Logo