My new gem for creating rspec proxies

1 minute read

I already wrote a lot about test proxies (here, here and here).

I just took the time to transform my previous gist in a full fledged ruby gem. It’s called “rspecproxies” and it can be found on github. It’s fully tested, documented and there’s a usage section in the readme to help anyone get started.

Here are the pain points proxies try to fix :

  • Without mocks, it is sometimes just awfully painfull to write the test (do you really want to start a background task just to get a completion ratio ?)
  • With classic stubs, you sometimes have to stub things you are not interested in in your test, you end up with unmaintainable extra long stub setup

Let’s have a look at a few examples of tests with proxies :

  • Verify actual load count without interfering in any behaviour
it 'caches users' do
  users = User.capture_results_from(:load)

  controller.login('joe', 'secret')
  controller.login('joe', 'secret')

  expect(users).to have_exactly(1).items
  • Use proxies to stub an object that does not yet exist
it 'rounds the completion ratio' do
   RenderingTask.proxy_chain(:load, :completion_ratio) {|s| s.and_return(0.2523) }

   expect(response).to include('25%')

I’d really love to see more code tested with proxies, it makes the whole testing so much more natural. As with any testing techniques, we get more thorough testing from the ease of writing the test.

I usually write about 15 minutes worth of reading per month. I won't transfer your email. No Spam, unsubscribe whenever you want.

As a gift for subscribing, you'll receive an illustrated mini-ebook "How to start a team coding dojo"!

Leave a comment