rspec shared examples

# Define a shared example group for objects that respond to certain methods
RSpec.shared_examples "responds to basic methods" do |object|
  it "responds to #foo" do
    expect(object).to respond_to(:foo)
  end

  it "responds to #bar" do
    expect(object).to respond_to(:bar)
  end
end

# Implementing the shared example group in a spec file
RSpec.describe MyClass do
  it_behaves_like "responds to basic methods", MyClass.new
end

Explanation:

  1. RSpec.shared_examples "responds to basic methods" do |object|
  2. Defines a shared example group named "responds to basic methods" that takes an object as a parameter.

  3. it "responds to #foo" do ... end

  4. Inside the shared example group, defines an example that checks whether the provided object responds to the #foo method using RSpec's expect syntax.

  5. it "responds to #bar" do ... end

  6. Another example within the shared example group, verifying whether the object responds to the #bar method.

  7. RSpec.describe MyClass do ... end

  8. Defines an RSpec example group for MyClass.

  9. it_behaves_like "responds to basic methods", MyClass.new

  10. Uses the shared example group responds to basic methods by calling it_behaves_like, passing in MyClass.new as the object to be tested against the shared examples. This ensures that instances of MyClass respond to #foo and #bar methods as specified in the shared examples.