Guard rspec-rails:仅运行模型规格

时间:2019-07-08 22:56:48

标签: ruby-on-rails ruby rspec guard

我们正在使用Guard gem在Rails 5应用程序上自动运行规范。如何配置它以仅运行型号规格?我们的配置是:

guard :rspec, cmd: 'spring rspec -p', parallel: true, failed_mode: :focus do

我们试图将其更改为

guard :rspec, cmd: 'spring rspec ./spec/models/ -p', parallel: true, failed_mode: :focus do

但它仍然可以运行所有测试。

1 个答案:

答案 0 :(得分:0)

在Guardfile中查找以下任何行:

  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { "spec/features" }
  watch(%r{^app/models/(.+)\.rb$})  { "spec/features" }
  watch(rails.controllers) do |m|
    [
      rspec.spec.call("routing/#{m[1]}_routing"),
      rspec.spec.call("controllers/#{m[1]}_controller"),
      rspec.spec.call("acceptance/#{m[1]}")
    ]
  end

例如以下行:

watch(%r{^app/models/(.+)\.rb$})  { "spec/features" }

如您所见,它会监视models文件夹中发生的任何更改,然后运行spec / features

替换为:

watch(%r{^app/models/(.+)\.rb$})  { "spec/models" }

仅当更改模型上的任何文件时,才会运行模型规范。

这是我正在使用的Gaurdfile,以防它对您有所帮助

guard :rspec, cmd: "bundle exec rspec" do
  require "guard/rspec/dsl"
  dsl = Guard::RSpec::Dsl.new(self)

  # RSpec files
  rspec = dsl.rspec
  watch(rspec.spec_helper) { rspec.spec_dir }
  watch(rspec.spec_support) { rspec.spec_dir }
  watch(rspec.spec_files)

  # Ruby files
  ruby = dsl.ruby
  dsl.watch_spec_files_for(ruby.lib_files)

  # Rails files
  rails = dsl.rails(view_extensions: %w(erb haml slim))
  dsl.watch_spec_files_for(rails.app_files)
  dsl.watch_spec_files_for(rails.views)

  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { "spec/features" }
  watch(%r{^app/models/(.+)\.rb$})  { "spec/features" }
  watch(rails.controllers) do |m|
    [
      rspec.spec.call("routing/#{m[1]}_routing"),
      rspec.spec.call("controllers/#{m[1]}_controller"),
      rspec.spec.call("acceptance/#{m[1]}")
    ]
  end

  # Rails config changes
  watch(rails.spec_helper)     { rspec.spec_dir }
  watch(rails.routes)          { "#{rspec.spec_dir}/routing" }
  watch(rails.routes)          { "spec"  } # { "#{rspec.spec_dir}/routing"  }
  watch(rails.app_controller)  { "#{rspec.spec_dir}/controllers" }

  # Capybara features specs
  watch(rails.view_dirs)     { "spec/features"  } # { |m| rspec.spec.call("features/#{m[1]}")  }
  watch(rails.layouts)       { |m| rspec.spec.call("features/#{m[1]}") }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
    Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
  end
end

如果您决定使用此脚本,则只需替换我上面提到的行,同时我建议您进行自己的配置,因为Guardfile会根据每个人的需求而有所不同