DRYing re-specs in RSpec

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

Leaving aside why such a repetition was necessary in the first place, I wonder how this can be dried.

I tried to turn blocks into objects and name them under Bar and Baz, but maybe because I didn't get the correct areas, I couldn't get it to 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
+5
source share

All Articles