Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear working of shared_examples inside a context in a shared_context #3076

Closed
lvonk opened this issue Apr 11, 2024 · 4 comments
Closed

Unclear working of shared_examples inside a context in a shared_context #3076

lvonk opened this issue Apr 11, 2024 · 4 comments

Comments

@lvonk
Copy link

lvonk commented Apr 11, 2024

Subject of the issue

Adding a context inside a shared_context breaks the working of include_examples.

Your environment

  • Ruby version: 3.2
  • rspec-core version: 3.12

Steps to reproduce

RSpec.shared_examples 'shared examples' do
  let(:foos) { [bar] }

  it 'uses the foo provided by the context' do
    expect(foos.length).to eq 2
  end
end

RSpec.shared_context 'shared context' do
  context 'my context' do
    include_examples 'shared examples'
  end
end

describe 'my test' do
  include_context 'shared context' do
    let(:foos) { [1, 2] }
  end
end

Expected behavior

The test to pass.

The test will pass if I change the shared_context (and optionally spec) to

RSpec.shared_context 'shared context' do
  include_examples 'shared examples'
end

describe 'my test' do
  context 'my context' do
    include_context 'shared context' do
      let(:foos) { [1, 2] }
    end
  end
end

Actual behavior

NameError: undefined local variable or method `bar' for #<RSpec::ExampleGroups::MyTest::MyContext "uses the foo provided by the context" (./spec/foo_spec.rb:2)>

  0) my test my context uses the foo provded by the context
     Failure/Error: let(:foos) { [bar] }

     NameError:
       undefined local variable or method `bar' for #<RSpec::ExampleGroups::MyTest::MyContext "uses the foo provided by the context"

Is this a bug or can't you use a context within a shared_context like this?

@lvonk lvonk changed the title Unclear (or inconsistent) working of shared_examples inside shared_context Unclear (or inconsistent) working of shared_examples inside a context in a shared_context Apr 11, 2024
@lvonk lvonk changed the title Unclear (or inconsistent) working of shared_examples inside a context in a shared_context Unclear working of shared_examples inside a context in a shared_context Apr 11, 2024
@pirj
Copy link
Member

pirj commented Apr 11, 2024

It may not be obvious at a glance, but the [bar] foos is defined closes to the example itself than the one in a block passed to include_context. And this works as intended.
I don’t feel that this is a good way of parameterizing an included example group, but can’t advise on a better approach in such a simplified/obfuscated context.

@pirj pirj closed this as not planned Won't fix, can't repro, duplicate, stale Apr 11, 2024
@lvonk
Copy link
Author

lvonk commented Apr 12, 2024

Perhaps this examples helps clarify what I want to achieve:

LineItem = Data.define(:amount, :quantity)

module SharedExamples
  LINE_ITEMS = 'Line items'

  RSpec.shared_examples LINE_ITEMS do
    let(:expected_payroll_line_item) { LineItem.new(amount: 10, quantity: 1) }

    let(:expected_line_items) { [expected_line_item_for_subscription, expected_payroll_line_item] }

    it 'creates the correct line items for subscription and payroll' do
      expect(system_under_test).to eq(expected_line_items)
    end
  end
end

module SharedContext
  BILLING = 'Billing'

  RSpec.shared_context BILLING, shared_context: :metadata do |_args = {}|
    let(:current_date) { Date.new(2024, 5, 1) }

    include_examples SharedExamples::LINE_ITEMS

    context 'before may 2024' do
      let(:current_date) { Date.new(2024, 4, 1) }

      include_examples SharedExamples::LINE_ITEMS do
        let(:expected_payroll_line_item) { LineItem.new(amount: 10, quantity: 2) }
      end
    end
  end
end

describe 'billing spec' do
  let(:system_under_test) do
    next [] if plan == 'b'

    if current_date < Date.new(2024, 5, 1)
      [LineItem.new(amount: 5, quantity: 1), LineItem.new(amount: 10, quantity: 2)]
    else
      [LineItem.new(amount: 5, quantity: 1), LineItem.new(amount: 10, quantity: 1)]
    end
  end

  context 'plan A' do
    let(:plan) { 'a' }
    let(:expected_line_item_for_subscription) { LineItem.new(amount: 5, quantity: 1) }
    include_context SharedContext::BILLING
  end

  context 'plan B' do
    let(:plan) { 'b' }
    include_context SharedContext::BILLING do
      let(:expected_line_items) { [] }
    end
  end
end

So the test describes for which plan I expect which line_items. The shared context defines several contexts with tests within a certain plan. The shared examples contains the test and assertion to execute each time.

If you have any advice to improve this that would be really helpful.

@JonRowe
Copy link
Member

JonRowe commented Apr 12, 2024

The indirection is whats causing you issues there, both contexts and examples are creating contexts so the lets in the examples take precedence, you could do a method define check in the examples or you could restructure it:

# frozen_string_literal: true

require 'date'

LineItem = Data.define(:amount, :quantity)

module SharedExamples
  LINE_ITEMS = 'Line items'

  RSpec.shared_examples LINE_ITEMS do
    it 'creates the correct line items for subscription and payroll' do
      expect(system_under_test).to eq(expected_line_items)
    end
  end
end

module SharedContext
  BILLING = 'Billing'

  RSpec.shared_context BILLING, shared_context: :metadata do |_args = {}|
    let(:current_date) { Date.new(2024, 5, 1) }
    let(:expected_payroll_line_item) { LineItem.new(amount: 10, quantity: 1) }
    let(:expected_line_items) { [expected_line_item_for_subscription, expected_payroll_line_item] }

    include_examples SharedExamples::LINE_ITEMS

    context 'before may 2024' do
      let(:current_date) { Date.new(2024, 4, 1) }
      let(:expected_payroll_line_item) { LineItem.new(amount: 10, quantity: 2) }

      include_examples SharedExamples::LINE_ITEMS
    end
  end
end

RSpec.describe 'billing spec' do
  let(:system_under_test) do
    next [] if plan == 'b'

    if current_date < Date.new(2024, 5, 1)
      [LineItem.new(amount: 5, quantity: 1), LineItem.new(amount: 10, quantity: 2)]
    else
      [LineItem.new(amount: 5, quantity: 1), LineItem.new(amount: 10, quantity: 1)]
    end
  end

  context 'plan A' do
    let(:plan) { 'a' }
    let(:expected_line_item_for_subscription) { LineItem.new(amount: 5, quantity: 1) }
    include_context SharedContext::BILLING
  end

  context 'plan B' do
    let(:plan) { 'b' }
    include_context SharedContext::BILLING do
      let(:expected_line_items) { [] }
    end
  end
end

@lvonk
Copy link
Author

lvonk commented Apr 15, 2024

Thanks for your advice, I really appreciate it. I went for the restructuring approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants