![]() For all you know, it could be something that saves you time and frustration that commonly occurs in your projects. describe 'some tests' do before do eeze (Time. Next time you look at the release notes for a project, don’t skip over the less featured functionality. Freeze the time before the test and unfreeze it after. travel_to is a great addition to the framework that would’ve otherwise required dragging in additional dependencies. Test "sends reminders on tuesday for those scheduled on wednesday" do While the change doesn’t look significant, it removed my need for the Timecop gem entirely.Īdditionally, the same test could be written as: test "sends reminders on tuesday for those scheduled on wednesday" do Supply a block automatically calls travel_back. ![]() Using the travel_to method with the example above results in the following: test "sends reminders on tuesday for those scheduled on wednesday" doīecause I’m using the travel_to method with a block, there’s no need to use the travel_back method to reset time back to the current time. The TimeHelpers test module adds the travel_to and travel_back methods to assist in stubbing Time.now and Date.today. Towards the bottom, though, was a feature barely mentioned or written about: Like many, I looked over the release notes for Rails 4.1 and features like Spring, secrets, ActionMailer previews, and ActiveRecord enums jumped out at me. He mentioned them in one of our various conversations about testing (he’s writing a book titled What Do I Test?). I think I first heard about some additional testing methods from my friend Eric Steele. Note: Chronic is a gem that helps create dates/times by using standard language. eeze Chronic.parse("4/15/14") doĪssert_equal 3, ActionMailer:: ![]() Prior to utilizing the changes in Rails 4.1, one of my tests looked like: test "sends reminders on tuesday for those scheduled on wednesday" do However, dragging in another gem was the last thing I wanted to do for something relatively simple. Timecop performed valiantly and did more than I ever needed (I generally only used the freeze method). For instance, if using RSpec, ensure to have a Timecop.return configured to run after all examples: in, e.g. It provides a unified method to mock Time.now. This new helper method forces the current time to whatever you specify, allowing you to make asserts against a historical time, or week in my case. A gem providing time travel and time freezing capabilities, making it dead simple to test time-dependent code. This is important because without control over the time, flakey tests can emerge in your codebase. With the release of Rails 4.1, the time stubbing method travel_to was added. Timecopis the go-to gem for testing time-dependent code, as it allows you to manipulate the current time during test runs. Timecop::Rspec provides Timecop time-machines for RSpec that allow you to time-travel test examples, context/describes, and/or your entire test suite. Naturally, I dragged in the timecop gem to handle freezing time, so my I could properly assert that certain events took place in the tests. ![]() The app is heavily dependent on times and recurring events (weekly). This can cause unanticipated problems if benchmark or other timing calls are executed, which implicitly expect.
0 Comments
Leave a Reply. |