cgore.comcgore@cgore.commicroblogblogphotosChristianitycryptographyjournalingoutdoorsmy master's thesisprogramming
Thinking Bicycle

Chris Gore: Programming: Ruby: RSpec Change Blocks

I use RSpec a lot for testing when I am in Ruby, and one nice feature that I just figured out is that the change term can take a block as the argument. The normal form is something like this:

expect { @something.add(12) }
.to change(@something, :value).from(0).to(12)

But, if you need to pass arguments to the method that will indicate the change that has occurred, then you will need to pass a block to change. Here is an example:

@repo = Repo.new
.from(false).to(true)
expect { some_action }.to change { something }.from(old).to(new)