在ruby中,您可以在irb中执行assert_equal和其他断言吗?
你可以在 irb 中执行 assert_equal 吗?这是行不通的。
require 'test/unit'
assert_equal(5,5)
Can you execute assert_equal from within irb? This does not work.
require 'test/unit'
assert_equal(5,5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然可以!
发生的事情是所有断言都是 Test::Unit::Assertions 模块中的方法。从 irb 内部扩展该模块使这些方法可以作为
main
上的类方法使用,这样您就可以直接从 irb 提示符中调用它们。 (实际上,在任何上下文中调用extend SomeModule
都会将该模块中的方法放在您可以从同一上下文中调用它们的位置 -main
恰好位于默认情况下您所在的位置.)此外,由于断言被设计为从 TestCase 内运行,因此语义可能与预期略有不同:它不是返回 true 或 false,而是返回 nil 或引发错误。
Sure you can!
What's going on is that all the assertions are methods in the Test::Unit::Assertions module. Extending that module from inside irb makes those methods available as class methods on
main
, which allows you to call them directly from your irb prompt. (Really, callingextend SomeModule
in any context will put the methods in that module somewhere you can call them from the same context -main
just happens to be where you are by default.)Also, since the assertions were designed to be run from within a
TestCase
, the semantics might be a little different than expected: instead of returning true or false, it returns nil or raises an error.正确答案是,
The Correct answer is ,
您还可以执行“
我不在正在测试的代码中使用
assert
”,我只在测试代码中使用它。You can also do
I don't use
assert
in code that's being tested, I only use it in test code.