-
-
Notifications
You must be signed in to change notification settings - Fork 753
Conversation
I'm not sure how I feel about the stubs /cc @xaviershay |
I'd like to investigate removing the explicit dependency on time before merging this, there might be a nicer way to do it. |
(i.e. have a "clock" configuration / dependency inject or something) |
We could also use something like timecop and do |
I like @myronmarston's idea here, simulate a long spec by shifting time forward within the spec. |
RSpec uses |
Oh right...we use We could do similar stubbing on |
The injection works… ok. TBH I'm not sure how much less brittle it is since it depends on a specific use pattern of the clock. |
|
||
before do | ||
# make it look slow without actually taking up precious time | ||
clock = double('RSpec::Core::Time') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be better to use class_double(RSpec::Core::Time)
. class_double
to give the guarantees of verified doubles, and the constant (rather than string name) since the class is already loaded and using the constant will give a clear error if the class is renamed.
FWIW I'm generally not a fan of timecop (except where unavoidable). I've never regretted decoupling my code from time. |
Uses |
example("example") { sleep 0.01 } | ||
example("example") do |example| | ||
# make it look slow without actually taking up precious time | ||
example.clock = class_double(RSpec::Core::Time, :now => RSpec::Core::Time + 0.5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's interesting that this is apparently working since I missed the .now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, how is that working? I tried this in a debugger session:
(rdb:1) p RSpec::Core::Time + 0.5
*** NoMethodError Exception: undefined method `+' for RSpec::Core::Time:Class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just use Time.now + 0.5
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why not use a much larger value such as 500
? A half second isn't very long and if someone set a debugger breakpoint to look at what's going on they could cause the faster example to take longer than the 0.5 seconds of the slower example to fail. Since no actual time passes you might as well use more exaggerated values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used RSpec::Core::Time
to be internally consistent with how the time gets calculated inside of Example
. In the unlikely case of us changing how RSpec::Core::Time
works, this spec might get re-examined because it uses it, which is probably a good thing.
I chose 0.5 so that I wouldn't have to change the specs expecting output like this:
expect(output.string).to match(/0(\.\d+)? seconds/)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me.
sleep not being deterministic has caused some test failures. Fixes #1282
LGTM. The rbx build just flaked out so I kicked it. Merge when green. |
Remove sleeps from formatter specs
I've rebased my PR on top of this, thanks @soulcutter! |
I'm not sure stubbing is the best way to fix this, let me know if you have a better idea.
Also something is failing for me in
command_line/order_spec.rb
which can't possibly be related, can it?