本文介绍了如何在 ruby​​ 代码中放置断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用断言并将评估放在我的 ruby​​ 代码中(例如:检查是否创建了 zip 文件、标签是否存在、文本区域中的消息等).我已经放了一些像 assert @selenium.is_text_present(textMessage) 这样的断言语句,但它们不起作用.

I want to use the assertions and put valuidations in my ruby code (e.g: checking if a zip file is created, label is present, message in the text area, etc). I have put a few assert statements like assert @selenium.is_text_present(textMessage), but they don't work.

如果要安装任何用于断言的 ruby​​ gem,请告诉我.

Please let me know if any ruby gem for assertions is to be installed.

推荐答案

对于简单的断言,您可能最好使用自己的断言方法来获取一个块:

For simple asserts, you're probably best off rolling your own assert method taking a block:

ruby-1.9.1-p378 > class AssertionError < RuntimeError
ruby-1.9.1-p378 ?>  end
 => nil 
ruby-1.9.1-p378 > def assert &block
ruby-1.9.1-p378 ?>  raise AssertionError unless yield
ruby-1.9.1-p378 ?>  end
 => nil 
ruby-1.9.1-p378 > assert { 1 > 0 }
 => nil 
ruby-1.9.1-p378 > assert { 5 == 12 }
AssertionError: AssertionError
    from (irb):8:in `assert'
    from (irb):11
    from /Users/mr/.rvm/rubies/ruby-1.9.1-p378/bin/irb:17:in `<main>'

以可复制的形式:

class AssertionError < RuntimeError
end

def assert &block
    raise AssertionError unless yield
end

i = 1
assert {i >= 0}
assert { 5 == 12 }

这篇关于如何在 ruby​​ 代码中放置断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:02