Thursday, 21 October 2010

Shared Examples group on RSpec (Drying up)


When you are drying up your RSpec, shared example group could become very handy, since it can help you gather (hence the name) the common / similar behaviors applied in your methods/ classes. All you need to do is factor out the common behaviors, put it in a shared example group and than use it in pairs with it_should_behave_like (add let(s) if you need to parameterized your specs). For example :

shared_examples_for "a shape" do
  it { should have(expected_n_items).sides}
  
  it "should have a color" do
      # ...
  end

  it "should have a center point" do
      # ...
  end
end
And call it like so :
describe "a cube" do
  let(:expected_n_items) { 6 }

  it_should_behave_like "a shape"

  it "should be 3D" do
      # ...
  end
end


Call them again for orb, prism, and so on ^^.

No comments:

Post a Comment