Guard – Why Won’t You Auto-Run My Tests! (Fixed!)
I’ve been having this problem recently. I can’t figure out why Guard will not auto-run my tests when I change a file. On old projects I could edit a file, save it, and Guard would run tests on that file. Worked great. Jump forward to my recent projects. I have to manually run my tests and it’s a PITA.
I’m running Ruby 2.1, Ruby 2.1.1, Rails 4.0.3.
New Rails project
rails new test-project
Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.0.3'
gem 'sqlite3'
gem 'guard'
gem 'guard-minitest'
Guardfile:
guard :minitest do
# with Minitest::Unit
watch(%r{^test/(.*)\/?test_(.*)\.rb$})
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
watch(%r{^test/test_helper\.rb$}) { 'test' }
# Rails 4
watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
watch(%r{^test/.+_test\.rb$})
watch(%r{^test/test_helper\.rb$}) { 'test' }
end
Output when I change a file:
11:23:26 - DEBUG - Stop interactor
11:23:26 - DEBUG - Hook :run_on_additions_begin executed for Guard::Minitest
11:23:26 - DEBUG - Hook :run_on_additions_end executed for Guard::Minitest
11:23:26 - DEBUG - Start interactor
Output when I press [ENTER]:
11:31:27 - INFO - Run all
11:31:27 - DEBUG - Hook :run_all_begin executed for Guard::Minitest
11:31:27 - INFO - Running: all tests
11:31:27 - DEBUG - Command execution: bundle exec ruby -I"test" -I"spec" -r bundler/setup -r minitest/autorun -r ./test/controllers/thisisatest_controller_test.rb -r ./test/helpers/thisisatest_helper_test.rb -r ./test/test_helper.rb -r /home/phill/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/guard-minitest-2.2.0/lib/guard/minitest/runners/old_runner.rb -e "" --
Run options: --seed 3898
# Running tests:
.
Finished tests in 0.047909s, 20.8730 tests/s, 20.8730 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
11:31:28 - DEBUG - Hook :run_all_end executed for Guard::Minitest
Here is my GIST of the problem: https://gist.github.com/Phill/9215877
Update!
I found this thread https://github.com/guard/listen/issues/155 that solved my problem. It had nothing to do with Guard/Listen. It was my text editor. I’m using Sublime Text 3 and with Atomic Saves turned on it would not trigger Listen for changes. Turning it off in the config fixed the problems.
"atomic_save": false