Rails / RSpec:'旅行'时间助手在功能规格中不可用?

时间:2016-11-08 12:45:28

标签: ruby-on-rails ruby-on-rails-4 rspec ruby-on-rails-5

我只是尝试使用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

2 个答案:

答案 0 :(得分:18)

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

您可以通过将其包含在单个测试套件中来实现此目的:

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

或全球:

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

答案 1 :(得分:3)

关注@ andrey-deineko ......

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

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