【问题标题】:running Guard rspec for ruby kata为 ruby​​ kata 运行 Guard rspec
【发布时间】:2012-12-27 05:51:06
【问题描述】:

所以我试图将guard rspec 仅用于ruby,而不是用于rails。测试运行良好,但唯一的问题是它只查找 spec.rb 文件,而不查找实际文件。所以我有一个失败的arabic_to_roman.rb 和arabic_to_roman_spec.rb。 Guardfile 安装在 roman_numerals_kata 目录下。它只查找 _spec.rb 文件而不是 arabic_to_roman.rb。这是 Guardfile 的样子

  1 # A sample Guardfile
  2 # More info at https://github.com/guard/guard#readme
  3 
  4 guard 'rspec' do
  5   watch(%r{^spec/.+_spec\.rb$})
  6   watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  7   watch('spec/spec_helper.rb')  { "spec" }
  8 
  9   # Rails example
 10   watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
 11   watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |m| "spec/#{m[1]}#     {m[2]}_spec.rb" }
 12   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"] }
 13   watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
 14   watch('config/routes.rb')                           { "spec/routing" }
 15   watch('app/controllers/application_controller.rb')  { "spec/controllers" }
 16 
 17   # Capybara features specs
 18   watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/features/#{m[1]}_spec.rb" }
 19 
 20   # Turnip features and steps
 21   watch(%r{^spec/acceptance/(.+)\.feature$})
 22   watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23 end
24 

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    你的目录结构是什么?

    基于此配置,Guard 会在 lib 目录中查找任何 ruby​​ 文件,并将执行嵌套在 spec/lib/ 中的匹配测试。这些详细信息在保护文件的这一行中:watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }

    使用这个 Guardfile(与我的标准项目布局相匹配),您应该具有以下文件夹布局:

    BASE_DIR
    ├── Guardfile
    ├── lib
    │   ├── README
    │   └── arabic_to_roman.rb
    ├── spec
    │   ├── lib
    │   │   └── arabic_to_roman_spec.rb
    │   └── spec_helper.rb
    

    如果不希望符合标准文件夹布局,另一种方法是像这样修改 Guardfile

    guard 'rspec' do
      watch(%r{^spec/.+_spec\.rb$})
      watch(%r{^(.+)\.rb$})     { |m| "spec/#{m[1]}_spec.rb" }
      watch('spec/spec_helper.rb')  { "spec" }
    end
    

    请注意,第二个 watch 命令在 basedir 中而不是在 lib/* 中查找它

    希望这会有所帮助:)

    【讨论】:

    • 目录结构是arabic_numerals_kata Guardfile arabic_to_roman.rb spec arabic_to_roman_spec.rb 这应该没问题吧?我还发现,如果我在 arabic_to_roman_spec.rb 文件中编写代码,它就可以工作。因此,出于某种原因,它似乎只扫描 _spec 文件而不是实际的 arabic_to_roman.rb 文件。
    • arabic_to_roman.rb 应该嵌套在 lib 文件夹中,以便根据 Guardfile 设置进行查看。或者将 Guardfile 中的行更改为:watch(%r{^(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }。我建议模仿默认文件夹布局,尽管如我的回答中所示,而不是像我刚才那样调整保护文件。
    • 当这解决了您的问题时,请将问题标记为已回答,以帮助以后提出类似问题的人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 2019-11-18
    • 1970-01-01
    • 2011-10-16
    • 2018-10-27
    相关资源
    最近更新 更多