A proxy that can be used to stub method chains and more!


Remember when RSpec had stub_chain? They removed it for good reasons but sometimes you just need it. Well, here it is, a proxy object. It doesn't actually mock anything for you (the name is just catchy) so you need to do that. But that actually comes with a lot of benefits:

  1. It's compatable with any testing framework
  2. You can use it for purposes other than testing, e.g. prototyping, code stubs
  3. Flexibility in how you use it without overloading the number of methods you have to remember

Here's an example usage:

let(:model_proxy) do {
    create_email: {
      receive: proc {}
before { allow(Model).to receive(:new).and_return model_proxy }
it 'should call receive' do
  proc = MockProxy.get(model_proxy, 'email_client.create_email.receive')
  expect(proc).to receive(:call)
  MockProxy.update(mock_proxy, 'email_client.create_email.validate!') { true }
  MockProxy.observe(mock_proxy, 'email_client.create_email.send') do |to|
    expect(to).to eq '[email protected]'

As you can see, the proc - which ends the proxy by calling the proc - can be used for anything. You can spy on the call count and arguments, mock methods, or just stub out code you don't want executed. Because it doesn't make any assumptions, it becomes very flexible. Simple, yet powerful, it's uses are infinite. Enjoy


Add this line to your application's Gemfile:

gem 'mock_proxy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mock_proxy


All different types of test doubles, as found on wikipedia (so you know I did my homework)

# Stubs
proxy = { chain: { ends_with: proc { |*args| return 'stuff_here' } } })
allow(object).to receive(:method).and_return proxy
# Mocks
proxy = { chain: { ends_with: proc { |*args| return 'stuff_here' } } })
proc = MockProxy.get(proxy, 'method.chain.ends_with')
expect(proc).to receive(:call).with('some', 'arg').twice
# Spies
called_args = []
call_count = 0
proxy = { chain: { ends_with: proc { |*args| called_args << args; call_count += 1 } } })
expect(call_count).to >= 1
expect(called_args).to include ['first_args', 2, 3]
# Fakes
model = double('model')
proxy = proc { model }, where: { first: proc { model } })
# Dummy
proxy = proc {})


After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.