DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

shared_examples in RSpec

Pattern1: No arguments for it_should_behave_like

shared_examples_for "happy" do
  it{expect(dog.feel_good?).to be true}
end

context 'good status' do
  it_should_behave_like("happy"){ let(:dog){ Factorybot.create :dog, status: :eating }}
  it_should_behave_like("happy"){ let(:dog){ Factorybot.create :dog, status: :excited }}
  it_should_behave_like("happy"){ let(:dog){ Factorybot.create :dog, status: :running }}
end

Pattern2: with an argument for it_should_behave_like

shared_examples_for "0-23 hour" do |expectation|
  it{expect(hour.save).to be expectation}
end

describe 'hours' do
  it_should_behave_like("0-23 hour", false){ let(:hour){ Factorybot.create :hour, hour: -1 }}
  it_should_behave_like("0-23 hour", true) { let(:hour){ Factorybot.create :hour, hour: 0 }}
  it_should_behave_like("0-23 hour", true) { let(:hour){ Factorybot.create :hour, hour: 1 }}
  it_should_behave_like("0-23 hour", true) { let(:hour){ Factorybot.create :hour, hour: 22 }}
  it_should_behave_like("0-23 hour", true) { let(:hour){ Factorybot.create :hour, hour: 23 }}
  it_should_behave_like("0-23 hour", false){ let(:hour){ Factorybot.create :hour, hour: 24 }}
  it_should_behave_like("0-23 hour", false){ let(:hour){ Factorybot.create :hour, hour: 25 }}
end

🔗 Parent Note

Top comments (0)