Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write methods that insert rspec examples?

In a bunch of rspec rails unit specifications I do something like:

describe Foo do
  [:bar, :baz].each do |a|
    it "should have many #{a}" do
      Foo.should have_many(a)
    end
  end
end

For cleaner code I'd rather do something like:

describe Foo do
  spec_has_many Foo, :bar, :baz
end

So how do I write a helper method like spec_has_many() for inserting DSL code like rspec's it() method? If it were for an ordinary instance method I'd do something like:

def spec_has_many(model, *args)
  args.each do |a|
    define_method("it_should_have_many_#{a}") do
      model.should have_many(a)
    end
  end
end

What would be the equivalent for defining rspec examples?

like image 996
Mori Avatar asked Oct 20 '10 02:10

Mori


1 Answers

Ok, this took some messing around, but I think I got it working. It's a bit of metaprogramming hackery, and I personally would just use the first thing you described, but it's what you wanted :P

module ExampleMacros
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    # This will be available as a "Class Macro" in the included class
    def should_have_many(*args)
      args.each do |a|
        # Runs the 'it' block in the context of the current instance
        instance_eval do
          # This is just normal RSpec code at this point
          it "should have_many #{a.to_s}" do
            subject.should have_many(a)
          end
        end
      end
    end
  end
end

describe Foo do
  # Include the module which will define the should_have_many method
  # Can be done automatically in RSpec configuration (see below)
  include ExampleMacros

  # This may or may not be required, but the should_have_many method expects
  # subject to be defined (which it is by default, but this just makes sure
  # it's what we expect)
  subject { Foo }

  # And off we go. Note that you don't need to pass it a model
  should_have_many :a, :b
end

My specs fail because Foo doesn't have a has_many? method, but both tests run, so it should work.

You can define (and rename) the ExampleMacros module in your spec_helper.rb file and it will be available for inclusion. You want to call include ExampleMacros in your describe blocks (and not any others).

To make all of your specs include the module automatically, configure RSpec like so:

# RSpec 2.0.0
RSpec.configure do |c|
  c.include ExampleMacros
end
like image 91
Robert Speicher Avatar answered Sep 25 '22 08:09

Robert Speicher