Giter Club home page Giter Club logo

Comments (8)

richievos avatar richievos commented on July 23, 2024 3

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.

jtrupiano avatar jtrupiano commented on July 23, 2024

@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.

richievos avatar richievos commented on July 23, 2024

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.

jtrupiano avatar jtrupiano commented on July 23, 2024

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.

richievos avatar richievos commented on July 23, 2024

No problem. While non-optimal, I think putting a note in the readme would go a long way.

from timecop.

clarkdevis avatar clarkdevis commented on July 23, 2024

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.

pcasaretto avatar pcasaretto commented on July 23, 2024

Did the first thing (block auto-return) ever get fixed?

from timecop.

travisjeffery avatar travisjeffery commented on July 23, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.