Indika K
Indika K

Reputation: 1414

Testing a ruby block with minitest

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

Upvotes: 0

Views: 1067

Answers (1)

Myron Marston
Myron Marston

Reputation: 21820

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.

Upvotes: 4

Related Questions