-
-
Notifications
You must be signed in to change notification settings - Fork 763
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
Comments
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. |
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. |
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:
|
Thanks for your advice, I really appreciate it. I went for the restructuring approach. |
Subject of the issue
Adding a
context
inside ashared_context
breaks the working ofinclude_examples
.Your environment
Steps to reproduce
Expected behavior
The test to pass.
The test will pass if I change the
shared_context
(and optionally spec) toActual behavior
Is this a bug or can't you use a
context
within ashared_context
like this?The text was updated successfully, but these errors were encountered: