views:

30

answers:

1

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?

+3  A: 

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
rspeicher
That instance_eval() solution looks good. But is there a way to do without the extend ExampleMacros line? I don't have to include or extend anything to use has_many() in my models. Why is that, and could that work here? (P.S. The has_many matcher is from shoulda's rspec matchers.)
Mori
Updated my answer to resolve your comment.
rspeicher
Beautiful, thanks!
Mori
If that solved it, please accept the answer when you can.
rspeicher