我只是在我的一项功能规格中尝试使用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 ...
我在time_helpers.rb
下创建了文件spec\support
,如下所示:
RSpec.configure do |config|
config.include ActiveSupport::Testing::TimeHelpers
end