2022-03-04 Ruby assertionメソッドをirbで使う
assertion 系のメソッドを irb で雑に呼びたいなって思って調べた。
結論
これで使えるようになる。
require 'test/unit'
include Test::Unit::Assertions
※ Ruby 3.1.1
使ってみる
こんな感じ。
require 'test/unit'
include Test::Unit::Assertions
> assert 1 == 2
# ... .rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/test-unit-3.5.3/lib/test/unit/assertions.rb:60:in `block in assert_block': <false> is not true. (Test::Unit::AssertionFailedError)
> assert_equal 1, 2
# ... .rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/test-unit-3.5.3/lib/test/unit/assertions.rb:60:in `block in assert_block': <1> expected but was (Test::Unit::AssertionFailedError)
# <2>.
> assert_empty "a"
# ... /.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/test-unit-3.5.3/lib/test/unit/assertions.rb:60:in `block in assert_block': <"a"> was expected to be empty. (Test::Unit::AssertionFailedError)
Assertionメソッドたち
下記のドキュメントを参照されたし。
Module: Test::Unit::Assertions — test-unit - test-unit
参考
testunit - In ruby, can you execute assert_equal and other asserts while in irb? - Stack Overflow