0

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

4

1 回答 1

4

RSpec's yield matchers are simple syntactic sugar over a fairly simple way of testing blocks.

  1. Initialize a local variable before the block
  2. Call the method, and pass a block that mutates the local variable you declared in #1.
  3. 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.

于 2013-03-30T21:02:12.250 回答