DRY specs with ProgressiveRequirements
April 13, 2012 - 1 Comment - ruby rails testing rspec
Anyone who has spent significant time crafting automated tests with RSpec or Shoulda knows context nesting hell. This seems to be a common idiom with specs which are building up toward a successful scenario while testing failing scenarios along the way. This makes sense, because you’re testing different layers of requirements which seems perfectly suited to nesting contexts. That is, until you need to read the resulting spec, or make modifications to it down the road.
Here’s what I’m talking about:
Note that I’m using a regular method for request_params rather than let or let!. This is necessary so I can use super and keep the requirements DRY. You can’t access the parent context with let (at least not yet).
Now, this isn’t half bad. But imagine if you had more than just three requirements to satisfy, and each one had its own consequence for not being met. Now imagine you wanted to also share these requirements so they could be reused, mixed and matched for different controller actions. Now you have to extract them into shared exampl…
