Giter Site home page Giter Site logo

Comments (4)

pirj avatar pirj commented on July 29, 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.

from rspec-core.

lvonk avatar lvonk commented on July 29, 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.

from rspec-core.

JonRowe avatar JonRowe commented on July 29, 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

from rspec-core.

lvonk avatar lvonk commented on July 29, 2024

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

from rspec-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.