
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
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:
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
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 = 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)
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.
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.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that mock_proxy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.