-
-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: freeze time along with http state #266
Comments
Digging in, I see that HTTPInteraction now captures |
A bit more on the topic: I can get the functionality I want with:
The problem then is deciding when and how to call
Works, but slightly messy. Unless there's a clearly better way, perhaps I'll post a StackOverflow Q&A. |
This sounds like a useful feature. It would need to be optional, though. Do you have an idea for what the API should be? |
Maybe just add a
|
I like that or maybe |
As a minor point, I like the As for the mocking library, I'm not sure I understand your question: is timecop a 'mocking library'? Can rspec-mocks and mocha freeze time in the same way that timecop can? Regardless, you could make it a configuration parameters, such as:
Having glanced at delorean and time_travel, I'd not complain if VCR only supported timecop -- it seems the best thought out and supported. But having said as much, it's worth pointing out that the user doesn't need to call any timecop methods within the use_cassette block, so you're not really exposing the timecop package. In fact, VCR could implement time-freezing in just a few lines of code. |
You're right; it's more of a stubbing library.
It would be great if this could work without involving a stubbing library like timecop or any of the others. My concern here is that to properly freeze time, you've got to:
Safely stubbing and restoring methods can get complicated (I've worked a lot in rspec-mocks and seen the complications), so I'm not sure how you'd get all this to work with "just a few lines of code". And actually, VCR is virtually monkey-patch free, and I'd like to keep it that way. What if VCR.use_cassette("cassette_name") do |cassette|
Timecop.freeze(casssette.recorded_at) do
# do stuff
end
end ...which isn't much more code than the a cassette option, doesn't add any additional monkey patching to, and doesn't couple VCR to anything new. |
That feels exactly right, especially since cassette is already passed as an argument to the block. I like it!! |
+1 this would be useful!
then in the test
|
👍 for exposing the |
@markevans -- you touch on one of the complexities of this (which is a big reason I haven't taken a stab at implementing it yet): how to handle the case where there is no recorded time. You probably still want to freeze time as your example shows. Would something like this be sufficient? VCR.use_cassette("some/cassette") do |cassette|
Timecop.freeze(cassette.recorded_at || Time.now) do
# do stuff
end
end Basically, make |
@myronmarston yes, I think so. It's probably better that the user knows what they're actually doing rather than VCR do magic behind the scenes, and having recorded_at return nil when not recorded is a valid interface in itself I think. |
@markevans -- sounds good. One other little thing...I'm thinking maybe the method should be called |
Agreed. It would be nice if there were a way to provide a null object rather than nil for easier debugging, but I can't think of a straightforward way to do that. I think raising a "NotRecorded" exception would be a bad idea, as it makes it more difficult to code for (rescues and whatnot). |
One other idea: VCR.use_cassette("some/cassette") do |cassette|
Timecop.freeze(cassette.first_recorded_at { Time.now }) do
# do stuff
end
end Make |
Good point - yes first_recorded_at is clear and more meaningful. Personally I'm not keen on the block syntax as the returned block value (e.g. Time.now) has nothing to do with the "first recorded value", so in my view shouldn't feature as part of that method call. If raising a NotRecorded exception (which is a valid alternative to returning nil I think) then rather than using that exception as control flow, I'd favour doing
which I know is verbose, but seems more correct somehow |
I'm not against the block format. It follows with the Hash syntax for setting default values. I could go either way on the |
Correct me if I'm wrong, but Myron could implement the block style to raise On Sun, Jun 9, 2013 at 11:09 AM, Nathaniel Bibler
|
Honestly, I'm on the fence about if the block makes for a better API or not. I'd definitely want the method to support a block if we made it default to raise an error if the cassette had not already been recorded, so that users can override what it should do in that case. I'm OK with just returning |
sorry I meant to put although I personally prefer just returning |
OK, I've implemented this in #335. I decided to call the method |
nice one thanks! |
Since some web sites have time dependencies, it would be useful if, upon playback, VCR could set Ruby's time and date to correspond to the time that the original cassette was recorded.
I'm imagining a construct such as:
I haven't recently looked at timecop, delorean, time-travel, etc, which presumably could do something similar. But freezing time is closely related to freezing web state, so it makes sense to incorporate it into VCR.
The text was updated successfully, but these errors were encountered: