Personally when I come across cases like this I either break the (usually 
public) method into a public and internal method, the internal method contains 
the date so we can test the edge cases.

E.g. MyClass>> doSomethingWorthTesting 
...
basicDoSomethingWorthTestingAt: DateAndTime now.
...

My test then uses the internal method for testing where I can pass in different 
dates.


Another option which you've probably decided against is to delegate to your 
current class or a Registry to ask for the class with which to use for date 
related tasks, a kind of factory in a way. 
MyClass>>>> doSomethingWorthTesting 
...
now := self class dateClass now.
...


I personally feel Pharo is moving into a more pluggable direction with the work 
on the environment (Smalltalk environment) where we can provide our own 
environment for a particular context, something similar to Newspeaks nested 
classes and imports using the usingPlatform: construct.
E.g.
class MyClassModule usingPlatform: platform = (
private DateAndTime = platform time DateAndTime.
private Error = platform exceptions Error.
)

Hmm maybe we can do a lightweight version using DynamicBindling library
myEnvironmentBinding bindDuring: [
...your testcase...
]

Cheers
Carlo

On 17 Jul 2011, at 11:45 PM, Sean P. DeNigris wrote:

I keep coming up against this situation over and over in testing.

MyClass>>doSomethingWorthTesting
   ...
   now := DateAndTime now.
   ...

I want to control DateAndTime class>>now to make this test determinate and
simulate interesting edge cases, but can't figure out a good way. In Ruby, I
could easily stub it because it's not a live system (or use dependency
injection), but here I don't want to mess with the real DateAndTime class in
case something goes wrong and leaves my system dirty.

I really hate using dependency injection for this kind of thing (especially
without Ruby's default parameter values). I've been creating MyClass>>now
and stubbing that. Am I being paranoid, should I just hijack the real class
and fix it when I'm done? What's the best way to do this?

Thanks.
Sean

--
View this message in context: 
http://forum.world.st/Stubbing-class-side-methods-tp3674065p3674065.html
Sent from the Pharo Smalltalk mailing list archive at Nabble.com.



Reply via email to