4

我正在尝试在带有一些 Ruby 文件和 RSpec 测试的小型 Rub​​y 中创建一个 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 。(需要明确的是,我没有使用那个宝石。它只是一个容易找到守卫的地方。)

4

1 回答 1

8

安装guard-rspecgem 后,它应该“正常工作”。

以下是我为使其干净加载而执行的步骤:

▶ 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'
于 2013-09-18T05:57:46.633 回答