What would be the best way of testing a Ruby block with minitest. Rspec seems to have a set of yield matchers. Is there something similar in minitest
Asked
Active
Viewed 1,052 times
1 Answers
4
RSpec's yield matchers are simple syntactic sugar over a fairly simple way of testing blocks.
- Initialize a local variable before the block
- Call the method, and pass a block that mutates the local variable you declared in #1.
- Verify the value of the variable afterwards.
So, you can do something like this:
block_called = false
do_something { block_called = true }
assert_true block_called
Alternately, if you like the syntax and failure output of rspec-expectations, you can easily use it with minitest (or any other testing framework); I blogged about this if you want the nitty-gritty details.

Myron Marston
- 21,452
- 5
- 64
- 63