Yo Ludke
Yo Ludke

Reputation: 2259

Example Groups with the same expectation

A logged in user has access to a resource and can get there in different ways. I want to have an example group, that each test for the same expectation.

I put an page.should have_content("...") expectation in an after(:each) block, but that is not such a good solution: If I declare it pending, it fails anyway. And if it fails, the error appears (at first) white.

How should I write example groups that each have the same expectation?

Upvotes: 1

Views: 1229

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84114

It sounds like you want a shared example group:

describe 'foo' do
  shared_examples "bar" do
    it 'should ...' do
    end
  end

  context "when viewing in the first way" do
    before(:each) do
        ...
    end

    it_behaves_like 'bar'
  end

  context "when viewing in the second way" do
    before(:each) do
        ...
    end

    it_behaves_like 'bar'
  end
end

Within the before blocks you set things up so that the action is taken out in the correct way. Another way of doing this is to have your shared examples call a do_foo method and provide different implementations of do_foo in each context.

You can also have shared contexts if what you want to share is the setup stuff.

Upvotes: 2

Related Questions