Often, similar assertions are made in multiple tests, which leads to duplication of code. One of the ways this can be addressed is with RSpec's shared example groups, which allow you to reuse a set of tests to fit different situations.
Let's say we have a few tests that repeat the same assertion:
describe AddressValidator it "valid? returns false for incomplete address" do expect(AddressValidator.valid?(address)).to eq(false) end context "address contains invalid characters" do let(:address) { "$123% Any^ St., Anytown, CA, USA 12345" } it "valid? returns false for incomplete address" do expect(AddressValidator.valid?(address)).to eq(false) end end context "address is a String" do let(:address) { "123 Any St., Anytown" } it "valid? returns false for incomplete address" do expect(AddressValidator.valid?(address)).to eq(false) end end end
We can see that we keep repeating the same assertion in each...