Shared examples

# support/shared_examples.rb
RSpec.shared_examples "somethingshared" do
end

# any_rspec.rb
RSpec.describe SubscriptionPlans::OneMonthPlan, type: :factory do
  it_behaves_like "somethingshared"
 end
RSpec.shared_examples "somethingshared" do |parameter|
end

Rspec.describe SomeClass do
  include_examples "somethingshared", "abc"
  include_examples "somethingshared", "bbc"
  include_examples "somethingshared", "ccc"
  # actually all above 3 shared examples' parameters will be override to "ccc", the last declaration wins
end

# avoid overriding parameter, using it_behaves_like
Rspec.describe SomeClass do
  it_behaves_like "somethingshared", "abc"
  it_behaves_like "somethingshared", "bbc"
  it_behaves_like "somethingshared", "ccc"
end

Reference

results matching ""

    No results matching ""