我正在尝试使用一些Ruby文件和RSpec测试在一个小Ruby中创建一个Guardfile。这不是Rails项目。
当我运行gem install guard
时,我得到以下内容:
$ gem install guard
Fetching: listen-1.3.1.gem (100%)
Successfully installed listen-1.3.1
Fetching: lumberjack-1.0.4.gem (100%)
Successfully installed lumberjack-1.0.4
Fetching: guard-1.8.3.gem (100%)
Successfully installed guard-1.8.3
Installing ri documentation for listen-1.3.1
Installing ri documentation for lumberjack-1.0.4
unable to convert "\xCF" from ASCII-8BIT to UTF-8 for bin/fsevent_watch_guard, skipping
Installing ri documentation for guard-1.8.3
3 gems installed
显然应该安装“\ xCF”,但事实并非如此。我无法弄清楚这是什么,以及这是否是我问题的可能原因。
稍后,当我尝试为RSpec测试创建一个防护时,会发生以下情况:
$ guard init rspec
19:45:11 - INFO - Writing new Guardfile to /home/kathryn/demo3/Guardfile
19:45:11 - ERROR - Could not load 'guard/rspec' or '~/.guard/templates/rspec' or find class Guard::Rspec
正如INFO建议的那样,创建了一个新的Guardfile,但是它上面填充了一条注释,指示我指向gem的README而不是RSpec的保护。如果我为RSpec手动添加防护,然后尝试运行guard
,结果是:
$ guard
19:41:03 - ERROR - Could not load 'guard/rspec' or find class Guard::Rspec
19:41:03 - ERROR - cannot load such file -- guard/rspec
19:41:03 - ERROR - Invalid Guardfile, original error is:
> [#] undefined method `new' for nil:NilClass
19:41:03 - ERROR - No guards found in Guardfile, please add at least one.
19:41:03 - INFO - Guard is using NotifySend to send notifications.
19:41:03 - INFO - Guard is using TerminalTitle to send notifications.
19:41:04 - INFO - Guard is now watching at '/home/kathryn/demo3'
我看到gem找不到它需要的文件,但我不确定从哪里开始。这是我第一次使用后卫。任何帮助表示赞赏。
这是我目前的Guardfile:
guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
这不是自动生成的。当我第一次创建Guardfile时,内容是:
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
我在这里添加了作为标准RubyGem项目守卫出现的RSpec守卫:https://github.com/guard/guard-rspec。 (要明确的是,我没有使用那颗宝石。这只是一个找到警卫的好地方。)
答案 0 :(得分:8)
安装guard-rspec
gem后,它应该“Just Work”。
以下是我正在采取的干净利落的步骤:
▶ gem install guard-rspec rspec
Fetching: rspec-core-2.14.5.gem (100%)
Successfully installed rspec-core-2.14.5
Fetching: diff-lcs-1.2.4.gem (100%)
Successfully installed diff-lcs-1.2.4
Fetching: rspec-expectations-2.14.2.gem (100%)
Successfully installed rspec-expectations-2.14.2
Fetching: rspec-mocks-2.14.3.gem (100%)
Successfully installed rspec-mocks-2.14.3
Fetching: rspec-2.14.1.gem (100%)
Successfully installed rspec-2.14.1
Fetching: guard-rspec-3.0.3.gem (100%)
Successfully installed guard-rspec-3.0.3
Parsing documentation for rspec-core-2.14.5
Installing ri documentation for rspec-core-2.14.5
Parsing documentation for diff-lcs-1.2.4
Installing ri documentation for diff-lcs-1.2.4
Parsing documentation for rspec-expectations-2.14.2
Installing ri documentation for rspec-expectations-2.14.2
Parsing documentation for rspec-mocks-2.14.3
Installing ri documentation for rspec-mocks-2.14.3
Parsing documentation for rspec-2.14.1
Installing ri documentation for rspec-2.14.1
Parsing documentation for guard-rspec-3.0.3
Installing ri documentation for guard-rspec-3.0.3
Done installing documentation for rspec-core, diff-lcs, rspec-expectations, rspec-mocks, rspec, guard-rspec after 15 seconds
Successfully installed rspec-2.14.1
Parsing documentation for rspec-2.14.1
Done installing documentation for rspec after 0 seconds
7 gems installed
完成后,我执行以下命令:
▶ guard init rspec
00:55:13 - INFO - rspec guard added to Guardfile, feel free to edit it
我的Guardfile看起来像这样:
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard :rspec 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|\.slim)$}) { |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|slim)$}) { |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' }
end
当我执行guard start
命令时,这些错误不显示:
▶ guard start
00:57:08 - INFO - Guard uses NotifySend to send notifications.
00:57:08 - INFO - Guard uses Tmux to send notifications.
00:57:08 - INFO - Guard uses TerminalTitle to send notifications.
00:57:09 - INFO - Guard::RSpec is running
00:57:09 - INFO - Guard is now watching at '/home/vgoff/my_gems'