Project

mock_proxy

0.0
No commit activity in last 3 years
No release in over 3 years
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 MockProxy.new(email_client: { create_email: { receive: proc {} } }) end 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) run_system_under_test 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 'stop@emailing.me' end run_system_under_test2 end 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
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.10
>= 10.0

Runtime

>= 4.2.5.1
 Project Readme

MockProxy

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

Description

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
  MockProxy.new(email_client: {
    create_email: {
      receive: proc {}
    }
  })
end
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)
  run_system_under_test
  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 'stop@emailing.me'
  end
  run_system_under_test2
end

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

Installation

Add this line to your application's Gemfile:

gem 'mock_proxy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mock_proxy

Usage

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

# Stubs
proxy = MockProxy.new(method: { chain: { ends_with: proc { |*args| return 'stuff_here' } } })
allow(object).to receive(:method).and_return proxy
run_system_under_test
# Mocks
proxy = MockProxy.new(method: { 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
run_system_under_test
# Spies
called_args = []
call_count = 0
proxy = MockProxy.new(method: { chain: { ends_with: proc { |*args| called_args << args; call_count += 1 } } })
run_system_under_test
expect(call_count).to >= 1
expect(called_args).to include ['first_args', 2, 3]
# Fakes
model = double('model')
proxy = MockProxy.new(find: proc { model }, where: { first: proc { model } })
run_system_under_test
# Dummy
proxy = MockProxy.new(to_s: proc {})
run_system_under_test(proxy)

Development

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 rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/matrinox/mock_proxy. 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.

License

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