20

I followed the Rails Tutorial on setting up automated tests with Guard and Spork. Every once in a while, especially when saving an unedited template in my editor, Guard will complain (full backtrace):

ERROR: Problem with watch action!
undefined method `singularize' for "layouts":String

My Guardfile:

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^lib/(.+)\.rb$})                           { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  do |m|
    ["spec/routing/#{m[1]}_routing_spec.rb",
     "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
     "spec/acceptance/#{m[1]}_spec.rb",
     "spec/requests/#{m[1].singularize}_pages_spec.rb"] ### Look here ###
  end
  watch(%r{^app/views/(.+)/}) do |m|
    "spec/requests/#{m[1].singularize}_pages_spec.rb" ### Look here ###
  end
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('spec/spec_helper.rb')                        { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }
  # Capybara request specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
end


guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' } do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch(%r{^config/environments/.+\.rb$})
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb')
  watch('test/test_helper.rb')
end

Guard doesn't complain if I restart, but restarting is getting a bit annoying; admittedly, not as annoying as running rspec every time I want a test.

  • I tried the suggestion in this post, but I think .autotest may be the wrong file for guard, since this isn't fixing the issue.
  • The only similar error I found with Google doesn't seem related.
Community
  • 1
  • 1
jrhorn424
  • 1,863
  • 1
  • 21
  • 24

1 Answers1

32

Actually, in the Rails tutorial they're adding require 'active_support/core_ext' at the top of the Guardfile.

I think this might fix your issue.

Also be sure to declare the spork guard before the rspec guard.

rymai
  • 723
  • 5
  • 7
  • 7
    Yep, `#singularize` is from [ActiveSupport::Inflector](http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html), so `require 'active_support/inflector'` is sufficient. – Netzpirat Mar 15 '12 at 17:23
  • 2
    Doh! If it was a full code example, I would have copied-and-pasted. :D Hopefully this will help the unlucky soul who has this problem in the future. Also, thanks for the tip about guarding spork first! – jrhorn424 Mar 15 '12 at 20:16
  • Thanks for figuring it out guys. It saved a lot of my time. – rohitmishra Jun 21 '12 at 15:22
  • Helped me too when I wanted to add a watch to factories and needed singularize. Love StackOverflow! It's made "Google the problem" so much easier. :) – Gerry Jul 01 '12 at 10:44
  • Thanks for the second tip ..helped me a lot ! – simha Jun 29 '13 at 20:07