【发布时间】:2015-05-09 18:06:15
【问题描述】:
我学习了 Michael Hartl (https://www.railstutorial.org/) 的 Ruby on 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
[开发环境] centos 6.6 导轨 4.0.5 红宝石 2.2.1 宝石“守卫”,“2.6.1” 宝石'guard-rspec','2.5.0' 宝石'spork-rails','4.0.0' gem 'guard-spork', '1.5.0'
【问题讨论】:
-
Guard 似乎运行良好,但您在第 11 行的
./spec/requests/static_pages_spec.rb中有一个失败的测试。如果您修复此问题并保存,guard 将自动重新运行测试。如果您正在逐字阅读第 3 版,则标题应为"Home | #{@base_title}",如 Listing 3.38 所示。 -
我修复了
./spec/requests/static_pages_spec.rb,但问题还没有解决。我重写了问题的源代码,请查看。 -
也许我理解错了,但 Guard 似乎在为你工作:
Guard is now watching at '/vagrant/sample_app'。当您编辑任何测试并保存时,Guard 会重新运行您的测试。例如,将"#{@base_title} | Home"放回去,保存,Guard 将自动运行您的测试,您将收到之前的 1 条失败消息。这不是发生在你身上吗?
标签: ruby-on-rails ruby rspec guard