Stubbing DateTime Stops Execution?

2 views
Skip to first unread message

Karl

unread,
Oct 27, 2009, 7:17:48 PM10/27/09
to mocha-developer
It's hard for me to put in all my test and code, but it seem that when
I add the following to my test, it is stopping the execution of a
loop:

DateTime.stubs(:now).returns(DateTime.parse
("2010-01-01T10:00:00"))

In a nutshell, I'm looping over an array in my code. I put a simple
puts("CODE RAN") in the loop. When the above stub is in my test, I
only see the first element of the loop (one CODE RAN prints). There
should be 14 iterations in the loop.

Comment the DateTime stub line and I see the expected 14 iterations.

I admit defeat. Why?

Karl

James Mead

unread,
Oct 28, 2009, 6:44:39 AM10/28/09
to mocha-d...@googlegroups.com
2009/10/27 Karl <threa...@gmail.com>:

Hi Karl,

It's going to be difficult to help you without more detail. What we
really need is a simple example of a test exhibiting the behaviour you
describe. One tactic for achieving this is to temporarily remove
irrelevant lines of code (from both test and code under test) a bit at
a time while trying to keep the same error occurring.

I've written a quick test [1] based on the information you've
supplied, but I don't see the error.

Otherwise the best I can offer is some questions that might help get
to the bottom of the problem :-

- Is there any logic in your code that relies on the value returned
from DateTime.now?
- What is the logic that controls the number of iterations of the loop?
- Is all of the code under test in the same thread as the test?
- How are you using Mocha e.g. gem, Rails plugin
- What version of Mocha are you using?
- What version of Ruby are you using?

Regards, James.

[1] http://gist.github.com/220411

Reply all
Reply to author
Forward
0 new messages