![]() ![]() The post Working with Dates and Times in Rails RSpec testing appeared first on CoinGecko Blog. Whether you are looking to manipulate the date/time into a timestamp of the future or past, or perhaps freezing time, the TimeHelpers module proves to be a viable tool at your disposal. The TimeHelpers is a convenient helper module that allows us to manipulate date/time within our testing environment. Firstly, in order to use the TimeHelpers you have to include them into your tests spec/spechelper.rb nfigure do config nfigure do config config. This eliminates the need to bring in third party libraries such as the TimeCop gem. In this article, we will go through how to use ActiveSupport::Testing::TimeHelpers in testing with date and time logic. In conclusion, TimeHelpers is a helper module that is supported natively in the Rails framework. Item = Item.create(name: "Chocolate", expiration_date: Time.current)Įxpect(post.published_at).to eq(Time.current) Time.current # => Sun, 15:34:49 EST -05:00Īlternatively, this method also accepts a block and freezes the time inside the block. timecop DESCRIPTION A gem providing 'time travel' and 'time freezing' capabilities, making it dead simple to test time-dependent code. You can call this method to freeze the time Time.current # => Sun, 15:34:49 EST -05:00 timecop - 3,234 0.9 Ruby rspec-timecop VS timecop A gem providing 'time travel', 'time freezing', and 'time acceleration' capabilities, making it simple to test time-dependent code. Returns to the original time, by removing the stubs added by travel and travel_to. This can cause unanticipated problems if benchmark or other timing calls are executed, which implicitly expect. Let’s look at the simple example below, given the Item class below, in order to test the expired? method, you will need to create two objects, i.e., non_expired_item and expired_item to test both the happy and unhappy path. When you are writing a test case for codes which involve time sensitive functionality, you often encounter the need to create multiple test objects with different date and time attributes in order to cover both ‘happy path’ and ‘unhappy path’. For instance, if using RSpec, ensure to have a Timecop.return configured to run after all examples: in, e.g. For example, if you need to check whether an email is sent out within an hour, you don’t want to sit there waiting for the actual time to pass.Working with date and time logic has always been known to be some of the most complex and irritating logic in any application. A couple of tips Ive found with time travel in tests that might be useful. It contains methods to change the speed at which time passes, which my be useful if your application needs something to happen as time passes. Rspec will reset the environment(including Timecop) after running each example. If you’re outside of a Rails application, perhaps writing a gem or using another framework, you won’t have access to these helpers without manually including Active Support, so timecop is still a brilliant choice.Įven inside a Rails application you might choose to use the timecop gem as it has some slightly enhanced functionality over the Active Support helpers. Given that Rails has included these helpers, there’s no reason to use timecop as you’d be adding a dependency for functionality that’s already been provided by the framework. I just had a problem running Timecop with RSpec's expect syntax when I was running: it 'updates :completedat' do eeze expect (pletedat).to eq () end Where the times weren't matching. As your tests run, real time elapses and this can sometimes lead to tests where the expected result and actual value can be seconds apart. local ( 1994 ) end after do travel_back end # your tests here end But why?įreezing time can be useful if you ever find yourself testing times or dates used inside your app. ![]() Describe "some set of tests to mock" do before do travel_to Time. ![]()
0 Comments
Leave a Reply. |