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
1 2 3 4 5 6 7 8
- Use proxies to stub an object that does not yet exist
1 2 3 4 5 6 7
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.