I’m trying to keep my specs clean and DRY, but I have some tests for an API that are identical except for which version of the API is being tested. I could repeat the specs simply using something like this:
%w( v1 v2 ).each do |version|
describe "Query #{version} API" do
it "responds with JSON"
# make the call using the version
end
end
end
But I’d like something a bit cleaner, and so I’ve written this method:
module RepetitivelyDescribe
def repetitively_describe(*args, &example_group_block)
options = args.extract_options!
options.delete(:for).each do |item|
item_args = args.collect(&:dup) + [options.dup]
item_args[0] << " [#{item}]"
describe(*item_args) do
example_group_block.call item
end
end
end
end
RSpec::Core::ExampleGroup.extend RepetitivelyDescribe
And then my test could look more like this:
repetitively_describe "Query API", :for => %( v1 v2 ) do |version|
it "responds with JSON"
# make the call using the version
end
end
I realise this is a little bit of pedantry, but it’s one less level of indentation, and if I’m going to be making this call a lot, I’d like to have it cleaner.
But of course, it’s not working quite as I’d like. The call to describe within my repetitively_describe doesn’t get logged to the RSpec output (when using the documentation format output), though the examples within do get repeated and use the version block argument as expected. Essentially, that level of context is lost (describe blocks outside and inside of the repetitively_describe block are kept).
There’s more detailed example code in a gist should it be needed. Any clues on why this isn’t quite working right?
So (apologies if I’m repeating stuff you already know) but every time you call describe/context rspec creates a new class that is a subclass of the current example group class (which eventually is a subclass of
RSpec::Core::ExampleGroup) and then usesmodule_evalto evaluate the block in the context of that class. If I runthen the output is
When you call
itrspec create anExampleobject and appends it to a class instance variable on self (the current example group). rspec also sticks the current example group in the example’s metadata, walking up this tree of example groups is what gives you the full description of the example.Your
repetitively_describemethod callsdescribe, so at the point that you callexample_group_block.call itemself is indeed the freshly created example group. When the proc gets evaluated, it of course remembers what the value ofselfwas when it was called so your calls toitare made to the example group that was current when repetitively_describe (easily verifiable by sprinkling some calls to check the value of self throughout your code). Similarly a call to describe adds the example group as a child of the outer example group, not the one created byrepetitively_describe.What you of course need to do is call
example_group_blockpreserving the correct value of self.with this change
outputs
["foo Query API [v1] responds with JSON", "foo Query API [v2] responds with JSON"]instead of["foo responds with JSON", "foo responds with JSON"]before the change.