Rails 5.1使用RSpec

时间:2017-05-30 18:18:55

标签: ruby-on-rails ruby rspec

在观看RailsConf video on ActionDispatch::systemTestCase后,我很高兴将其整合到我当前的应用中。目前,我们的测试套件设置使用以下内容:

很难让配置适用于我们当前的设置,但我们最终得到了它的工作,这主要归功于Avdi Grimm撰写的一篇题为Configuring database_cleaner with Rails, RSpec, Capybara, and Selenium的文章。

我希望使用rails 5.1中发布的rails的内置系统测试。由于rails现在内置了系统测试:我需要担心的是配置如下:

  • rspec
  • factory_girl

这就是因为ActionDispatch::systemTestCase负责capybaradatabase_cleaner,并且已经为selenium驱动程序配置了它。

例如:目前我的feature specs是这样编写的(Capybara在RSpec的背景下):

#spec/features/blogs/creating_blogs_spec.rb
require "rails_helper"

RSpec.feature "Logged in User can create a blog" do
  before do
    create(:logged_in_user)
  end

  scenario "successfully", js: true do
    ...
  end
end 

这就是配置了rspecdatabase_cleanerfactory_girlcapybara的测试套件的典型集成/功能/系统规范。我想将其转换为使用ActionDispatch::systemTestCase

但是:我想在ActionDispatch::systemTestCase

的上下文中使用RSpec

上面的RailsConf视频展示了ActionDispatch :: systemTestCase如何在rails'的上下文中工作。默认测试套件布局(例如minitest,测试位于test目录中),但它没有讨论如何在RSpec上下文中使用ActionDispatch :: systemTestCase。

我找不到任何有关制作铁轨的资源'可通过RSpec配置的内置系统测试,包括system testing section of the rails guides内。这可能吗?

2 个答案:

答案 0 :(得分:12)

2017年10月更新:

Rspec-rails 3.7现在与系统测试完全兼容。

基本上,您需要添加到spec_helper.rb

RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :rack_test
  end

  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome_headless
  end
end

https://medium.com/table-xi/a-quick-guide-to-rails-system-tests-in-rspec-b6e9e8a8b5f6

答案 1 :(得分:4)

根据RailsConf Talk:Teaching RSpec to Play nice with Rails,从'Domain.ZonalResult<Domain.PinkBoxMasksDataAbstract>'起, 与rails&#39;不兼容系统测试集成。

似乎正在通过this pull request处理兼容性。