web-dev-qa-db-ja.com

Rails / RSpec:「Travel」タイムヘルパーは機能仕様では利用できませんか?

機能仕様の1つで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

22
Flip

これらのヘルパーを使用するには、それらをテストに含める必要があります。

これは、単一のテストスイートに含めることで実行できます。

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

またはグローバルに:

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

@ andrey-deinekoをフォローしています...

ファイルを作成しましたtime_helpers.rbspec\support 次のように:

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