4

I have installed 'guard' and 'guard-rspec', I have also configured Guardfile (to watch changes in 'app/views' ) but when I run 'bundle exec guard' I always get this:

  vagrant@vagrant-debian-squeeze:/vagrant/sample_app$ bundle exec guard
  Guard could not detect any of the supported notification libraries.
  Guard is now watching at '/vagrant/sample_app'
  Guard::RSpec is running, with RSpec 2!
  Running all specs
  ........

  Finished in 0.97359 seconds
  8 examples, 0 failures
  >

It has finished with guard console prompt and if I edit some file from 'app/views/' (e.g. app/view/static_pages/home.html.erb) and save it, guard didn't display any spec output, just still wait for some console command.

I suppose it should display some rspec output after saving watched file.

Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.3'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.9.0'
  gem 'guard-rspec', '0.5.5'
  gem 'guard'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.4'
  gem 'coffee-rails', '3.2.2'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  gem 'therubyracer'

  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.0'

group :test do
  gem 'capybara', '1.1.2'
  # gem 'rb-inotify', '0.8.8'
  # gem 'libnotify', '0.5.9'
end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

Guardfile:

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

require 'active_support/core_ext'

guard 'rspec', :version => 2, :all_after_pass => false 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$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb", (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : "spec/requests/#{m[1].singularize}_pages_spec.rb")]}
  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/(.+)/}) do |m|
    "spec/requests/#{m[1].singularize}_pages_spec.rb"
  end
end

Guard watch rule for 'app/views' is

  watch(%r{^app/views/(.+)/}) do |m|
    "spec/requests/#{m[1].singularize}_pages_spec.rb"
  end

Runtime environment:

Debian-Squeeze 32bit Vagrant box

Btw. I'm learning a Rails form ruby-on-rails-tutorial-book and stuck at Automated tests with Guard

Any help is appreciate, thank you.

2
  • For what it's worth, I am running Guard and vagrant and have the same problem. One wrinkle - if I open and edit the file from the shared directory on my local machine, nothing happens. If I do the same thing from a session on the vm, Guard runs my specs twice. So I wonder if it has something to do with how Guard detects the file has changed that works unexpectedly with a vm in between. Commented Jun 15, 2012 at 1:25
  • From my perspective it doesn't matter if I had edited file on host machine or on my notebook, result was same, nothing happen. Anyway interesting note. Thank you. Commented Jun 15, 2012 at 7:40

4 Answers 4

9

Guard doesn't pick up filesystems changes made by the Vagrant host (Ubuntu in my case).

You can pass the -p option to force guard to poll the filesystem (increases CPU usage, constantly hits the hard disk and can cause your laptop temperature to rise — so not ideal but it works)

bundle exec guard -p
Sign up to request clarification or add additional context in comments.

2 Comments

To bring your resource usage (way) down, you can force a minimum interval between polls with the --latency / -l flag. bundle exec guard -p -l 10 seems to cut down a lot. Source
BTW, Guard can listen for file changes from the network. Your host OS can run a small listen script that forwards the filesystem changes over TCP to Guard. For more information: github.com/guard/guard#-o--listen-on-option
0

Uncomment the notification library in your test group, so your Gemfile looks like.

group :test do
  gem 'capybara', '1.1.2'
  gem 'rb-inotify', '0.8.8'
  gem 'libnotify', '0.5.9'
end

That ought to do it. Have fun with Rails Tutorial, it's great!

EDIT: That's assuming your running Linux. OS X and Windows have their own test libraries. Keep reading the Tutorial and you'll see it.

1 Comment

I did that, but I've got same result. After running 'bundle exec guard' a spec tests run and when they finished a console prompt is displayed. Didn't understand why, in railscasts.com/episodes/264-guard episode after changing a watched file a spec tests are run immediately.
0

Try this instead (from the Guardfile generated by guard init):

watch(%r{^app/views/(.+)/}) { |m| "spec/requests/#{m[1]}_spec.rb" }

The regular expression is correct, but it seems something about the filename 'spec/requests/#{m[1].singularize}_pages_spec.rb' in the block causes guard to fail silently as if the expression hadn't matched. I have no idea why.

1 Comment

It seems that Guard doesn't work for me at all. I tried your suggestion but I've got same result, Guard doesn't display anything just waiting for another command. Watchr works perfectly so I don't want to bother myself with another tricky Guard configuration (Yes I know Guard activelly developed project while Watchr is almost dead, but it does what I want). Anyway thank you for you answer.
-3

I have remove Guard and install Watchr and it seems that it makes his work correctly. This helped me out:

How to rails 3 and rspec 2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.