How to re-use (share) Rspec examples across files

# Put your examples in a file, NOTE, do not put in any spec file, for #example, shared_test_examples.rbshared_examples 'shared demo examples' do
it 'is shared test' do
# eip is passed by the let() block in the spec files
expect(eip).to eq '10.1.1.1'
end
end
==============# Spec A, let's name it test_a_spec.rb. Please pay attention to the #relative path of the requirerequire_relative './shared_test_examples.rb'

describe 'AWS Use' do
before do
# Do AWS deployment, e.g. get the eip of the instance
@eip = '10.1.1.1'
end

include_examples 'shared demo examples' do
# let() block pass the eip as parameter to the shared examples
let(:eip) {@eip}
end

end
=========# Spec B, let's name it test_b_spec.rb. Please pay attention to the #relative path of the requirerequire_relative './shared_test_examples.rb'

describe 'Azure Use' do
before do
# Do Azure deployment, e.g. get the eip
@eip = '10.1.1.1'
end

include_examples 'shared demo examples' do
# let() block pass the eip as parameter to the shared examples
let(:eip) {@eip}
end

end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store