Comments (8)
A workaround for this if you're using RSpec is to add to your spec_helper:
config.before(:each) do
Timecop.return
end
from timecop.
@JerryVos I think this is user error. Your code snippet should be:
Timecop.freeze(a_time) { Timecop.travel(1.second) { # code here } }
The version of the call where a block is not passed is a feature that I use in several places and won't seriously consider removing.
For #1, I see what you're saying and would consider a patch to correct it. Timecop essentially uses a stack (implemented by native arrays) of TimeStackItem's. When the block returns, it's just a matter of popping every TimeStackItem off the stack that is above itself.
For #2, I had always considered this to be impossible....am I supposed to detect that Timecop is being run from within Test::Unit?? How should I know when/if to implicitly return when I'm using Timecop e.g. from irb? Could you point me to actual code samples from other mocking libraries where they handle this particular problem?
from timecop.
I agree having to pass a block to all calls would be annoying, I don't want to pass a block to travel when I am in a freeze.
For #2, happy to provide examples. My main thought is I have never typed Mocha.return or RSpec.return in a test or teardown/after block, and I would not expect to need to do that with another mocking library.
Towards the implementation, I would assume there probably would have to be some detection of some sort to handle all apis. Realistically though, if Test::Unit was supported along with RSpec a majority of users would be covered (RSpec support might even be free if Test::Unit is done [at least in rails apps]). The libraries I am familiar with take this approach:
- mocha - if you look at mocha/lib/mocha/integration, mocha monkey patches itself into Test::Unit::TestCase and MiniTest::Unit::TestCase
- rspec - the library itself has a lib/interop folder where it's Test::Unit::TestCase interoperability lives
- shoulda - lib/shoulda.rb has the triggering of the RSpec/TestUnit interoperability
if defined? Spec
require 'shoulda/rspec'
else
require 'shoulda/test_unit'
end - spork - when you run spork --bootstrap it detects if you are using RSpec, and updates the proper files
Let me know if you'd like more or can't find those files
from timecop.
Hey Richie,
Haven't forgotten about this. Thanks for supplying direct references to the other mocking libraries for this functionality. I'm hoping to have some time over the holiday to look further into this.
from timecop.
No problem. While non-optimal, I think putting a note in the readme would go a long way.
from timecop.
Timecop.return
ap Time.zone.now
Timecop.freeze(1.day.ago) {ap Time.zone.now}
ap Time.zone.now
=> Wed, 31 Aug 2011 09:52:50 UTC +00:00
Timecop.return
ap Time.zone.now
Timecop.freeze 1.day.ago {ap Time.zone.now}
ap Time.zone.now
=> Tue, 30 Aug 2011 09:53:22 UTC +00:00
Timecop.return
ap Time.zone.now
Timecop.freeze 1.day.ago do ap Time.zone.now end
ap Time.zone.now
=> Wed, 31 Aug 2011 09:59:41 UTC +00:00
Such behavior is not obvious!
from timecop.
Did the first thing (block auto-return) ever get fixed?
from timecop.
@pcasaretto yep, it's fixed.
[19] pry(main)> Time.now
=> 2012-07-26 15:58:13 -0500
[20] pry(main)> Timecop.freeze { Timecop.travel(10.days) { puts Time.now } }
2012-08-05 15:58:31 -0500
=> 2012-07-26 15:58:31 -0500
[21] pry(main)> Time.now
=> 2012-07-26 15:58:38 -0500
If there's another problem please make a new issue.
from timecop.
Related Issues (20)
- Is call to Timecop.travel(some random time) atomic in nature? HOT 1
- Time#travel should be side-effect free and not resume system clock HOT 10
- .
- Timecop.thread_safe = true by default HOT 1
- Timecop handles GMT/UTC Time objects incorrectly HOT 2
- Doesn't work for `Rails.cache` with `expires_in` HOT 2
- ruby 3.1 support
- Date.strptime returns wrong month for %Y HOT 6
- %W is not handled properly HOT 1
- Ruby 2.5 seems to work? HOT 4
- Using timecop with psych 4.0.5, behavior differs between the test environment and other environments HOT 3
- Timecop.scale to mock existing times HOT 2
- Timecop.freeze not working with expect().to receive HOT 3
- Test
- Need a proper test for making sure we don't change timezones inadvertently
- DateTime parse without date not working properly HOT 5
- DateTime.parse("Wednesday 12:00") format is always returning midnight HOT 1
- Time.now sometimes has greater-than-nanosecond precision when called under Timecop.travel HOT 3
- DateTime.parse does not handle CWeek correctly
- Mocking `Process.clock_gettime` causes `Concurrent::IVar` with timeout to wait indefinitely HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from timecop.