功能规格中是否没有“旅行”时间助手?

问题描述 投票:22回答:2

我只是在我的一项功能规格中尝试使用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

ruby-on-rails ruby-on-rails-4 rspec ruby-on-rails-5 activesupport
2个回答
33
投票

为了使用这些帮助程序,您必须将它们包括在测试中。

您可以通过将其包含在单个测试套件中来完成此操作:

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

或全局:

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

4
投票

跟随@ andrey-deineko ...

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

RSpec.configure do |config|
  config.include ActiveSupport::Testing::TimeHelpers
end
© www.soinside.com 2019 - 2024. All rights reserved.