Rails / RSpec: Travel helper not available in specs?

I just tried using the Rails travel helper time method in one of the specifications of my function:

 scenario 'published_at allows to set a publishing date in the future' do magazine_article.update_attribute(:published_at, Time.now + 1.day) expect { visit magazine_article_path(magazine_article.magazine_category, magazine_article) } .to raise_error(ActiveRecord::RecordNotFound) travel 2.days do visit magazine_article_path(magazine_article.magazine_category, magazine_article) expect(page).to have_content('Super awesome article') end end 

This gives me this:

 NoMethodError: undefined method `travel' for #<RSpec::ExampleGroups::MagazineArticles::AsUser::Viewing:0x007f84a7a95640> 

What am I missing?

http://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html

+18
source share
2 answers

To use these helpers, you must include them in your tests.

You can do this by including it in one test suite:

 describe MyClass do include ActiveSupport::Testing::TimeHelpers end 

or globally:

 RSpec.configure do |config| config.include ActiveSupport::Testing::TimeHelpers end 
+27
source

Following @ andrey-deineko ...

I created the time_helpers.rb file under spec\support as follows:

 RSpec.configure do |config| config.include ActiveSupport::Testing::TimeHelpers end 
+4
source

Source: https://habr.com/ru/post/1259459/


All Articles