-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
What Ruby, Rails and RSpec versions are you using?
Ruby version: 2.6.5
Rails version: 6.0.2.1
RSpec version: 3.9
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
Rails 6.0.2.1
RSpec 3.9
- rspec-core 3.9.1
- rspec-expectations 3.9.0
- rspec-mocks 3.9.1
- rspec-rails 3.9.0
- rspec-support 3.9.2
Observed behaviour
Test helpers provided by Rails should be useable out of the box. Specifically, their Minitest lifecycle hooks should be honoured automatically, by default.
Expected behaviour
Some test helpers do not work out of the box, as their lifecycle hooks are not called.
Specifically, ActiveSupport::Testing::TimeHelpers
can be included, and freeze_time
will work, but TimeHelpers#after_teardown
is not called, meaning it doesn't clean up after itself as it is supposed to.
To get it to work requires including RSpec::Rails::MinitestLifecycleAdapter
, which is not obvious, and error prone.
See in-depth discussion and motivation in rubocop/rubocop-rails#38.
Can you provide an example app?
I have constructed an example usage of ActiveSupport::Testing::TimeHelpers#freeze_time
in this repository.
The test does not "fail", due to the difficulty of testing test hooks. However, it puts
messages which indicate if it is working or not.
TL;DR
describe 'TimeHelpers#freeze_time' do
it 'works' { freeze_time }
end
The test above doesn't work unless the following is added to spec/rails_helper.rb
config.include ActiveSupport::Testing::TimeHelpers
This is unfortunate, but acceptable. However, the TimeHelpers#after_teardown
hook fails to run unless we also add
config.include RSpec::Rails::MinitestLifecycleAdapter
This means time is left frozen after the test, and violates the contract of freeze_time
, which is supposed to always cleanup.