'Travel' 时间助手在功能规范中不可用吗?

Is the 'Travel' time helper not available in feature specs?

我刚刚尝试在我的一个功能规范中使用 Rails' 时间辅助方法 travel

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 

它给了我这个:

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

我错过了什么?

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

为了使用这些助手,您必须将它们包含在您的测试中。

您可以通过将其包含到单个测试套件中来做到这一点:

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

或全局:

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

关注@andrey-deineko...

我在spec\support下创建了一个文件time_helpers.rb如下:

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