I released Impersonator, a Ruby library to record and replay object interactions. Many times in the past, I had missed something like VCR when HTTP was not involved. So this time I went ahead, a wrote this little library.

When testing, I have come to dislike having to build canned responses for external services I can’t invoke directly. VCR blew my mind years ago by offering the best of both worlds when testing HTTP services:

  • Not having to build artificial synthetic responses, test against real ones.
  • Enjoy fast executions

However, not every service is HTTP-based, so many times I had missed something that worked at a higher level. Hey, I want to fake this method invocation and make it return this value when invoked. And that is what Impersonator does.

It looks like this:

  Impersonator.recording('calculator add') do
    calculator = Impersonator.impersonate(:add){ Calculator.new }
    assert_equal 5, calculator.add(2, 3)
  end 

The first time this code runs, it will instantiate a Calculator object normally and record its method invocations and returned values. The next executions, it will use a double that will replay the values without instantiating a Calculator object at all. It also reproduces yielded values and validates that methods are invoked the same number of times with the expected signature.

This year I’ve been working on an exciting Ruby thing I plan to release in the coming months. Impersonator is an extraction from this project, and it has already represented a huge step forward in terms of developer happiness for me, so I hope it can be useful to other developers too.