Shared Examples in Rspec Example Groups



12.5
Shared Examples:

We declare a shared example group with the shared_examples_for( ) method.


shared_examples_for "any pizza" do
  it "tastes really good" do
    @pizza.should taste_really_good
  end
  
  it "is available by the slice" do
    @pizza.should be_available_by_the_slice
  end
end

Once a shared example group is declared, we can include it in other
example groups with the it_behaves_like( ) method.


describe "New York style thin crust pizza" do
  before(:each) do
   @pizza = Pizza.new(:region => 'New York', :style => 'thin crust')
  end
  it_behaves_like "any pizza"
  it "has a really great sauce" do
   @pizza.should have_a_really_great_sauce
  end
end
describe "Chicago style stuffed pizza" do
  before(:each) do
   @pizza = Pizza.new(:region => 'Chicago', :style => 'stuffed')
  end
  it_behaves_like "any pizza"
  it "has a ton of cheese" do
   @pizza.should have_a_ton_of_cheese
  end
end

That produces this:


New York style thin crust pizza
  has a really great sauce
  behaves like any pizza
    tastes really good
    is available by the slice
Chicago style stuffed pizza
  has a ton of cheese
  behaves like any pizza
    tastes really good
    is available by the slice
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: ,
Posted in RSpec and Cucumber, TDD & BDD

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: