begin-rescue
中的hooks.rb
块下方无效。当有"结果"目录可用,它转到rescue
块,当它不到时,它转到begin
块。听起来很有趣,但这就是发生的事情。
现在当我在单独的ruby文件中使用相同的begin-rescue
时,它就像魅力一样。当目录可用时运行begin
,而当目录不可用时运行rescue
。
告诉你"结果"测试完成后出现的目录,因为我正在使用pretty_face
gem。它有几个子目录,如"图像"和"样式表"。
hooks.rb
require 'fileutils'
def delete_results
FileUtils.remove_dir(File.join(File.absolute_path('../..', File.dirname(__FILE__)),"results"))
end
Before do
begin
delete_results
rescue
puts "cant delete results dir or it doesnt exist"
end
end
我也在单独的ruby文件中尝试过,它始终有效: 的 delete_dir.rb
require 'fileutils'
begin
FileUtils.remove_dir(File.join(File.absolute_path('../..', File.dirname(__FILE__)),"results"))
rescue
puts "cant delete results dir or it doesnt exist"
end
当dir存在时出错但挂钩中的begin
阻止.rb无法正常工作
File exists - results\images (Errno::EEXIST)
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:247:in `mkdir'
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:247:in `fu_mkdir'
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:221:in `block (2 levels) in mkdir_p'
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:219:in `reverse_each'
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:219:in `block in mkdir_p'
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:205:in `each'
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:205:in `mkdir_p'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/pretty_face-0.10.3/lib/pretty_face/formatter/html.rb:188:in `make_directory'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/pretty_face-0.10.3/lib/pretty_face/formatter/html.rb:182:in `make_output_directories'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/pretty_face-0.10.3/lib/pretty_face/formatter/html.rb:63:in `before_features'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/lib/cucumber/ast/tree_walker.rb:181:in `block in send_to_all'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/lib/cucumber/ast/tree_walker.rb:179:in `each'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/lib/cucumber/ast/tree_walker.rb:179:in `send_to_all'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/lib/cucumber/ast/tree_walker.rb:169:in `broadcast'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/lib/cucumber/ast/tree_walker.rb:20:in `visit_features'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/lib/cucumber/runtime.rb:49:in `run!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/lib/cucumber/cli/main.rb:47:in `execute!'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.3.17/bin/cucumber:13:in `<top (required)>'
C:/Ruby193/bin/cucumber:23:in `load'
C:/Ruby193/bin/cucumber:23:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Process finished with exit code 1
Empty test suite.
错误即将发生,因为由于某些原因(我不知道),它无法删除目录,因为&#34;图像&#34;和&#34;样式表&#34; subdir存在于&#34;结果&#34; DIR。所以我手动删除了它们,以便begin
阻止工作。现在上面的错误不存在,它跳过begin
块并转到rescue
块并打印&#34;无法删除结果dir或它不存在&#34;虽然&#34;结果&#34; dir在那里。