Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guard rspec-rails: run only model specs

We are using Guard gem to automatically run specs on a Rails 5 app. 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.

like image 627
Daniel Cukier Avatar asked Nov 07 '22 15:11

Daniel Cukier


1 Answers

In Guardfile look for any lines like these:

  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

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

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

like image 173
Amir El-Bashary Avatar answered Nov 12 '22 13:11

Amir El-Bashary