![]() TLDR Since Ruby on Rails 5.2 timecop gem can be replaced by built-in methods defined within the ActiveSupport::Testing::TimeHelpers module. Please remember about unfreezing time in tests, regardless of an approach you choose. Sooner or later each of us encounters a situation where a method depends on time. Among Rubyists, the most popular gem which provides handy helpers to this problem is called timecop:Ī gem providing “time travel”, “time freezing”, and “time acceleration” capabilities, making it simple to test time-dependent code. This is super handy with timecop, a gem to mock Time. You can define arbitrary flags to toss at the end of a describe, it, context, etc., to run arbitrary code. ![]() To better illustrate when the gem may be useful, let’s write down some naive lines of code representing a building with a clock ⏰: class Building It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call. timecop DESCRIPTION A gem providing 'time travel' and 'time freezing' capabilities, making it dead simple to test time-dependent code. Automating timecop gem for testing time-dependent Ruby code By Staff Published ApRSpec is pretty cool. It 'returns time displayed by the clock' doĮxpect(described_).to eq Īnd, surprisingly or not, the test didn’t pass: expected: 19:26:14.958265000 +0000 To cover the #clock method with rspec we could use the below lines: describe Building do #Ruby timecop code You know what they say – time flies when you’re having fun ?. The timecop gem provides a set of useful method to handle such cases without a need for complicated mocking time-related objects, inter alia:
0 Comments
Leave a Reply. |