Timecop 0.3.4 has just been released. To install simply run: gem install timecop.
Timecop is a RubyGem providing “time travel” and “time freezing” capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.
Documentation is hosted at . The source code is hosted at .
Timecop 0.3.0 has just been released. To install simply run: gem install timecop.
Timecop is a RubyGem providing “time travel” and “time freezing” capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.
Documentation is on . The source code is hosted at .
Updates include:
API
Completely remove Timecop#unset_all (deprecated by Timecop#return in 0.2.0)
Return Time.now from #freeze, #travel and #return — code contributed by Keith Bennett ()
Maintenance
Fix bug that left Time#mock_time set in some instances
Upped build dependency to jeweler ~> 1.2.1
Don’t pollute top-level namespace with classes/constants
Documentation
Clearer examples in the README, better description in the gemspec
It’s very rare that I do any C++ programming these days. However, one of my oldest customers continues to utilize a C++-based optimization/statistics framework that we helped them build many years ago. The project has a , and we owe quite a bit to some of the first people to trust us (thank you Sommer and Dorry!).
As a Ruby programmer, I’ve come to love test-driven programming. As such, I’ve made an effort recently to build a test-based workflow into this existing codebase (not always the easiest thing to do, applying a test-base to a large existing codebase). Today I found myself in dire need of being able to test private functions in C++. As a testament to the poor state of testing in other programming languages, many message boards/threads simply told me that I was testing the wrong thing (you should test the public API, not the private implementation). Well, needless to say, this left me a bit uncomfortable. The fact is, the majority of my code is tucked into private methods, and I’d be left with huge long-running end-to-end tests if I strictly followed this heuristic.
However, I came across one golden nugget, one of the most clever hacks I’ve seen in some time. By utilizing pre-processor directives, we can temporarily override the meaning of private and protected in C++ code, essentially aliasing it to public.
See what this is doing? We wrap the class that we’ll be testing in pre-processor directives to interpret protected and private as an alias for public, essentially loading that class (when including the header file) as entirely public. This allows me to access everything! Private methods, private variables, you name it. And now, just like in Ruby, it’s no holds barred, allowing me to poke and prod my objects without being wrapped on the wrist by the compiler.
Even better, these directives simply wrap your includes in your test files. In other words, I don’t have to change my implementation to achieve this.
A clever hack, no matter the language or technology, is a clever hack. And I absolutely love this hack.
Credit: I discovered this technique as a comment on .
I just released version 0.2.0 of this evening (morning).
The primary feature added was the distinction between “freezing” and “rebasing” time. In 0.1.0, Timecop.travel would actually freeze time. This is no longer the case. Rather, a time offset will be calculated, and a running clock is simulated by always offsetting the time returned by Time.now (and friends) by the original offset.
(Note that time can still be frozen with Timecop.freeze.)