[rspec-users] Nice little DSL trick for extending ExampleGroups in RSpec

Kristian Mandrup kmandrup.github at gmail.com
Tue Aug 10 20:02:18 EDT 2010


describe 'migration_generator' do
  include RSpec::Rails::Orm::ActiveRecord
  include RSpec::Rails::Migration
...
end

Didn't quite feel right. So I made it prettier DSL like using the
solution below ;)


class Class
  def use_orm orm
    class_eval do
      include "RSpec::Rails::Orm::#{orm.to_s.camelize}".constantize
    end
  end

  def specify_for type
    raise ArgumentError, "Can not specify for #{type}" if !
[:migration, :model].include?(type)
    class_eval do
      include "RSpec::Rails::#{type.to_s.camelize}".constantize
    end
  end
end

describe 'migration_generator' do
  use_orm :active_record
  specify_for :migration

  ...
end

Could it be done in a better way without polluting Class or some other
low lv Ruby class?
I want the functions from the included module to be avalable within
all my: before, after, it, describe etc. blocks.


More information about the rspec-users mailing list