Drying repeated specs in RSpec

Posted by snl on Stack Overflow See other posts from Stack Overflow or by snl
Published on 2010-04-09T21:59:11Z Indexed on 2010/04/09 22:03 UTC
Read the original article Hit count: 338

Filed under:
|

In the test below, the Bar and Baz blocks contain identical specs.

Leaving aside why such repetition was necessary in the first place, I'm wondering how one could dry this up.

I tried turning the blocks into objects and calling them under Bar and Baz, but possibly because I did not get the scopes right, I have not been able to make it work.

describe Foo do
  describe Bar do
    before(:each) do
      prepare
    end

    it "should do something" do
      true
    end

    it "should do something else" do
      true
    end
  end

  describe Baz do
    before(:each) do
      prepare_something_else
    end

    it "should do something" do
      true
    end

    it "should do something else" do
      true
    end
  end
end

© Stack Overflow or respective owner

Related posts about rspec

Related posts about ruby