Categories: None [Edit]
mock_proxy
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
Total
Ranking: 23,120 of 184,487
Downloads: 58,746
Daily
Ranking: 35,677 of 184,472
Downloads: 2
Downloads Trends
Ranking Trends
Num of Versions Trends
Popular Versions (Major)
Popular Versions (Major.Minor)
Depended by
Rank | Downloads | Name |
---|
Depends on
Rank | Downloads | Name |
---|---|---|
1 | 2,587,986,982 | bundler |
7 | 1,048,720,099 | activesupport |
10 | 1,011,350,820 | rake |
Owners
# | Gravatar | Handle |
---|---|---|
1 | matrinox |