简体   繁体   English

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

[英]Guard rspec-rails: run only model specs

We are using Guard gem to automatically run specs on a Rails 5 app. 我们使用Guard gem在Rails 5应用程序上自动运行规范。 How to configure it to run only the models specs? 如何配置它只运行模型规格? Our config is: 我们的配置是:

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

We tried to change it to 我们试图将其改为

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

but it still runs all tests. 但它仍然运行所有测试。

In Guardfile look for any lines like these: 在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

This line for example: 这一行例如:

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

As you can see it watches for any changes happen to models folder then it run the spec/features 正如您所看到的,它会监视模型文件夹中发生的任何更改,然后运行spec / features

Replacing it with: 替换为:

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

Will run the models spec only when any file on models is changed. 仅当模型上的任何文件发生更改时才会运行模型规范。

This is my Gaurdfile which i'm using in case it might help you 这是我正在使用的Gaurd文件,以防它可能对你有所帮助

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

If you decided to use this script, just replace the lines i mentioned above, while i recommend that you go for your own configurations, since Guardfile meant to be different based on everyone's needs 如果您决定使用此脚本,只需替换上面提到的行,而我建议您选择自己的配置,因为Guardfile根据每个人的需要而有所不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM