fade2black
fade2black

Reputation: 614

Aliasing ExampleGroup

In RSpec I can give alias to examples. For instance, alias_example_to. Is there any way of aliasing Example Groups? I can use only describe and context. But I want to use, say, feature, scenario...etc. For example,

describe MyObject do
  scenario "doing smth with object" do
  ...
  end
end

I found an article on http://benediktdeicke.com/2013/01/custom-rspec-example-groups/. Is there any other way to alias Example Groups.

Upvotes: 0

Views: 90

Answers (2)

Benedikt Deicke
Benedikt Deicke

Reputation: 610

A possible workaround until the feature is released is this:

# spec/support/example_group_aliases.rb
module ExampleGroupAliases
  extend ActiveSupport::Concern

  included do
    class << self
      alias_method :simple, :context
    end
  end

  module ClassMethods
    def fancy(description, options = {}, &block)
      context(description, options.merge(:fancy => true), &block)
    end
  end

  RSpec.configure do |config|
    config.include self
  end
end

The code shows two ways of defining aliases for the context method. The first (simple) one is using alias_method. The second one (fancy) is defining a new method that then calls the original context method. The last approach allows you to do additional stuff, like adding some more options.

Upvotes: 1

Peter Alfvin
Peter Alfvin

Reputation: 29409

As I interpret github, this feature was requested via https://github.com/rspec/rspec-core/issues/493 and is awaiting integration via https://github.com/rspec/rspec-core/pull/870. It is not yet available.

Upvotes: 2

Related Questions