守门员不工作

Guard doesn't work

我学习了 Ruby Michael Hartl (https://www.railstutorial.org/) 的 Rails 教程。

在3.6.2中,我想使用Guard自动测试。我写了和本书一样的代码并修改了测试文件,但是测试没有自动运行。

这是执行Guard的消息

[vagrant@localhost sample_app]$ bundle exec guard
which: no notify-send in (/home/vagrant/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/bin:/home/vagrant/.rbenv/versions/2.2.1/bin:/usr/local/.rbenv/libexec:/home/vagrant/.rbenv/plugins/ruby-build/bin:/home/vagrant/.rbenv/shims:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/pgsql-9.3/bin:/usr/local/.rbenv/bin:/home/vagrant/bin)
14:32:55 - INFO - Guard is using TerminalTitle to send notifications.
14:32:55 - INFO - Starting Spork for RSpec
Using RSpec, Rails
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
14:32:59 - INFO - Spork server for RSpec successfully started

14:32:59 - INFO - Guard::RSpec is running
14:32:59 - INFO - Running all specs
Running tests with args ["--color", "--failure-exit-code", "2", "--format",     "progress", "--format", "Guard::RSpec::Formatter", "--require", "/home/vagrant/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/guard-rspec-2.5.0/lib/guard/rspec/formatter.rb", "spec"]...
...........

Finished in 0.6213 seconds
11 examples, 0 failures


Randomized with seed 44839

Done.

14:33:01 - INFO - Guard is now watching at '/vagrant/sample_app'

这是 Guardfile。

# A sample Guardfile
# More info at https://github.com/guard/guard#readme
require 'active_support/inflector'
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
  :rspec_env    => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end

guard 'rspec', 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{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |m| "spec/#{m[1]}#{m[2]}_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"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/features/#{m[1]}_spec.rb" }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }

  # Custom Rails Tutorial specs
  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",
     (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
         "spec/requests/#{m[1].singularize}_pages_spec.rb")]
  end
  watch(%r{^app/views/(.+)/}) do |m|
    (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
        "spec/requests/#{m[1].singularize}_pages_spec.rb")
  end
  watch(%r{^app/controllers/sessions_controller\.rb$}) do |m|
    "spec/requests/authentication_pages_spec.rb"
  end
end

这是static_pages_spec.rb

require 'spec_helper'

describe "Static pages" do

  subject { page }

  shared_examples_for "all static pages" do
    it { should have_content(heading) }
    it { should have_title(full_title(page_title)) }
  end

  describe "Home page" do
    before { visit root_path }
    let(:heading)    { 'Sample App' }
    let(:page_title) { '' }

    it_should_behave_like "all static pages"
    it { should_not have_title('| Home') }
  end

  describe "Help page" do
    before { visit help_path }
    let(:heading) {'Help'}
    let(:page_title) {'Help'}

    it_should_behave_like "all static pages"
  end

  describe "About page" do
    before { visit about_path }
    let(:heading) {'About'}
    let(:page_title) {'About Us'}

    it_should_behave_like "all static pages"
  end

  describe "Contact page" do
    before { visit contact_path }

    let(:heading) {'Contact'}
    let(:page_title) {'Contact'}

    it_should_behave_like "all static pages"
  end
end

[开发环境] 中心 6.6 Rails 4.0.5 ruby2.2.1 gem 'guard', '2.6.1' gem 'guard-rspec', '2.5.0' gem 'spork-rails', '4.0.0' gem 'guard-spork', '1.5.0'

我自己解决了这个问题。

试试这个命令。

bundle exec guard -p