警卫失败:“错误:退出代码1”(Spork和TestUnit)

时间:2014-01-21 15:20:31

标签: ruby ruby-on-rails-4 tdd guard spork

我是Rails上的TDD新手,我想要正确的工具。 TestUnit + Spork + Guard对我来说似乎很完美,但我无法让它发挥作用。设置似乎是正确的,但是当我启动Guard时,会发生这种情况:

Ruff% guard --debug
16:08:59 - DEBUG - Command execution: which notify-send
16:08:59 - DEBUG - Command execution: emacsclient --eval '1' 2> /dev/null || echo 'N/A'
16:08:59 - INFO - Guard is using NotifySend to send notifications.
16:08:59 - INFO - Guard is using TerminalTitle to send notifications.
16:08:59 - DEBUG - Command execution: hash stty
16:08:59 - DEBUG - Guard starts all plugins
16:08:59 - DEBUG - Hook :start_begin executed for Guard::Spork
16:08:59 - DEBUG - Command execution: ps aux | grep -v guard | awk '/spork/&&!/awk/{print $2;}'
16:08:59 - DEBUG - Killing Spork servers with PID: 
16:08:59 - INFO - Starting Spork for Test::Unit
16:08:59 - DEBUG - guard-spork command execution: ["exec", "spork", "testunit", "-p", "8988"]
Using TestUnit, Rails
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8988!
16:09:05 - INFO - Spork server for Test::Unit successfully started
16:09:05 - DEBUG - Command execution: notify-send Spork Test::Unit successfully started -t 3000 -h int:transient:1 -i /home/simplonco/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/guard-2.3.0/images/success.png -u low

16:09:05 - DEBUG - Hook :start_end executed for Guard::Spork
16:09:05 - DEBUG - Hook :start_begin executed for Guard::Test
16:09:05 - INFO - Guard::Test 2.0.4 is running, with Test::Unit 2.5.5!
16:09:05 - INFO - Running all tests
16:09:05 - INFO - Using testdrb to run the tests
16:09:05 - DEBUG - Command execution: testdrb -I"lib:test" 
Running tests with args ["-Ilib:test"]...
Usage: testrb [options] tests...
Error: exit code 1
Done.

“错误:退出代码1”让我发疯,Guard不会启动测试。我发现没有人有同样的问题。

当我修改文件时,Guard会识别它并启动自己。然后发生这种情况:

10:51:42 - DEBUG - Hook :run_on_modifications_end executed for Guard::Test
10:51:42 - DEBUG - Start interactor
10:58:06 - DEBUG - Stop interactor
10:58:06 - DEBUG - Hook :run_on_modifications_begin executed for Guard::Test
10:58:06 - INFO - Running: test/models/user_test.rb
10:58:06 - DEBUG - Command execution: testdrb -I"lib:test" 
Running tests with args ["-Ilib:test"]...
Usage: testrb [options] tests...
Error: exit code 1
Done.

我花了很多时间在守卫的文档上,找不到任何东西。 Bundle exec guard不能更好地工作。我试图从头开始创建一个新的应用程序:“错误:再次退出代码1”。

我的警卫档案:

guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, 
           :rspec_env => { 'RAILS_ENV' => 'test' } do


watch('Gemfile')
  watch('config/application.rb')
  watch('config/environment.rb')
  watch('config/environments/test.rb')
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb') { :rspec }
  watch('test/test_helper.rb') { :test_unit }
  watch(%r{features/support/}) { :cucumber }
end

guard :test, drb: true do
  watch(%r{^test/.+_test\.rb$})
  watch('test/test_helper.rb')  { 'test' }

  # Non-rails
  watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }

  # 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" }
end

我的gemfile的测试组:

group :test do
    gem 'turn'
    gem 'guard-test'
    gem 'guard-livereload'
    gem 'guard-spork' 
    gem 'spork-rails'
    gem 'spork-testunit'
end

1 个答案:

答案 0 :(得分:0)

查看spork-testunit我看到预期的用法是

testdrb -Itest test/your_test.rb

而Guard执行

testdrb -I"lib:test"

比较这两个,我看到Guard命令缺少测试的路径,这就是Spork抱怨的原因:

Usage: testrb [options] tests...
Error: exit code 1

这让我觉得你有一个Guard配置或项目结构/测试命名问题,但是如果没有看到你的Guardfile并且对你的测试文件有所了解就很难说。

一般使用test_path选项配置的Guard :: Test looks at your test path,默认为test。然后是tries to find your test files by a specific pattern,然后传递给命令执行。当然,只有在运行所有测试时才会这样。

模式是:

Dir[File.join(path, '**', 'test_*.rb')] +
Dir[File.join(path, '**', '*_test{s,}.rb')]

因此,根据您的测试文件命名模式,您可以在rails控制台中验证它:

rails c
[1] pry(main)> Dir[File.join('test', '**', '*_test{s,}.rb')]
=> []

这里我失败了,因为我使用了RSpec,但是如果一切正常,它应该返回所有测试文件的列表。