Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating RSpec example groups with different arguments

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?

like image 284
pat Avatar asked Jun 02 '12 09:06

pat


1 Answers

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 uses module_eval to evaluate the block in the context of that class. If I run

describe "foo" do
  puts "#{self}; #{self.superclass}"
  describe "behaviour 1" do
    puts "#{self}; #{self.superclass}"
    context "with x" do
      puts "#{self}; #{self.superclass}"
    end
  end
end

then the output is

#<Class:0x007fb772bfbc70>; RSpec::Core::ExampleGroup
#<Class:0x007fb772bfb180>; #<Class:0x007fb772bfbc70>
#<Class:0x007fb772bfa5f0>; #<Class:0x007fb772bfb180>

When you call it rspec create an Example object 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_describe method calls describe, so at the point that you call example_group_block.call item self is indeed the freshly created example group. When the proc gets evaluated, it of course remembers what the value of self was when it was called so your calls to it are 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 by repetitively_describe.

What you of course need to do is call example_group_block preserving the correct value of self.

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
        class_exec(item, &example_group_block)
      end
    end
  end
end

with this change

describe('foo') do
  repetitively_describe "Query API", :for => %w( v1 v2 ) do |version|
    it "responds with JSON"
  end
end.descendant_filtered_examples.collect(&:full_description)

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.

like image 120
Frederick Cheung Avatar answered Sep 17 '22 05:09

Frederick Cheung