guard / guard-rspec

Guard::RSpec automatically run your specs (much like autotest)

Home Page:https://rubygems.org/gems/guard-rspec

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Watch RSpec files in app directory

demisx opened this issue · comments

I want to phase out my Rspec unit tests to the same location as the files I am testing. I want them visually coupled. Something like this:

app/services/
  |___doctor_map_builder.rb
  |___doctor_map_builder_spec.rb

The rest of the specs (models, request etc.) will remain in the default spec/ directory. I am having hard time configuring Guardfile to kick off these unit tests in the new location. Here is what I've added to the Guardfile:

guard :rspec, all_after_pass: false do
  ...
  watch(%r{^app/(.+)_spec\.rb$}) 
end

but nothing happens when I update app/services/doctor_map_builder_spec.rb file. However, if I change to:

guard :rspec, all_after_pass: false do
  ...
  watch(%r{^app/(.+)_spec\.rb$}) { "app/services" }
end

then all specs in app/services directory run as expected (obviously, I don't want them all run - just the one that has changed). What am I missing please? I want rspec to run only for the unit test file in app/* that was changed. Thank you.

Have you try to add app folder to spec_paths: %w[app spec]?

Yes, it should help. Spec files are being filtered based on spec_paths option.
@thibaudgg Is this really needed to filter spec files (and feature files too)?

@907th good question, I can't remember why we added that, maybe for performance issue... but if it's more an annoyance we could remove it.

Beautiful. Thanks guys!

For Googlers, here is the configuration that also allows me to run unit specs in app directory upon their change (Awesome!):

guard :rspec, :spec_paths => ["spec", "app"], all_after_pass: false do
  ...
  watch(%r{^app/(.+)_spec\.rb$}) 
end