Rspec-core: Pending Shared Example

Created on 5 Jun 2014  ·  6Comments  ·  Source: rspec/rspec-core

I've got a collection of duck-type classes that all implement a set of methods.
I'm able to build a shared example group that describes a these methods.

shared_examples_for "a duck type" do
  it { should respond_to(:method1) }
end
describe Class1 do
  it_should_behave_like "a duck type"
end
describe Class2 do
  it_should_behave_like "a duck type"
end

In my situation, I have implemented Class1#method1, but Class2#method1 is pending.

I'd like the ability to say:

describe Class2 do
    it_should_behave_like "a duck type", :pending
end

I can apply pending to a shared example, but not to it_should_behave_like. This is sub-optimal when working with a collection of duck-types where some class implementations are complete and others are pending.

Enhancement requests:
1) enable the :pending attribute for it_should_behave_like
2) enable the :skip attribute for it_should_behave_like
3) add xit_should_behave_like

Most helpful comment

If anyone else finds this issue via Google, here's another trick you can use at the call site, which doesn't require modifying the shared examples definition to take a metadata or flags argument:

it_behaves_like 'some shared example' do
  before { pending }
end

All 6 comments

Arguments passed to it_should_behave_like have a different purpose: they get yielded to the shared example group block:

https://relishapp.com/rspec/rspec-core/v/3-0/docs/example-groups/shared-examples#passing-parameters-to-a-shared-example-group

It would be confusing to treat the arguments as metadata as well. You can get the behavior you want, though:

shared_examples_for "a duck type" do |*metadata|
  context "describe this group", *metadata do
    # put the specs in here.
  end
end

Alternately, if you don't want to wrap the specs in an additional context, you can support pending or skip with a before hook:

shared_examples_for "a duck type" do |*flags|
  before { pending } if flags.include?(:pending)
  # specs go here
end

Yes thanks very much - that will work perfectly.

If anyone else finds this issue via Google, here's another trick you can use at the call site, which doesn't require modifying the shared examples definition to take a metadata or flags argument:

it_behaves_like 'some shared example' do
  before { pending }
end

I believe the issue should be reopened and one of the following should be done

  • Either allow marking all examples as pending by wrapping them around pending blocks
pending('Reason for pending') do 
  it_behaves_like '...'
  context do 
     ...
  end
end

@andyl

Happy to accept a PR to improve the documentation if you'd like to attempt it, but I don't feel the api should be expanded to change this behaviour.

Was this page helpful?
0 / 5 - 0 ratings