RSpec.describe Array do subject {[1, 2, true, "c", "ruby"]} it "should have 1"do expect(subject.include?(1)).to eq(true) end subject {["Rails", :apple, 88, 1]} it "should have 1"do expect(subject.include?(1)).to eq(true) end subject {["true", false, 1, "c"]} it "should have 1"do expect(subject.include?(1)).to eq(true) end end
因此,我們可以把重複的 examples 抽離,並在需要 example 時, include_examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
RSpec.shared_examples "same_element"do it "should have 1"do expect(subject.include?(1)).to eq(true) end end RSpec.describe Array do subject {[1, 2, true, "c", "ruby"]} include_examples "same_element" subject {["Rails", :apple, 88, 1]} include_examples "same_element" subject {["true", false, 1, "c"]} include_examples "same_element" end