Skip to content

Commit

Permalink
fixes case when let and let_it_be declared together
Browse files Browse the repository at this point in the history
  • Loading branch information
lHydra committed May 17, 2024
1 parent 9b2c204 commit 5f0895b
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 77 deletions.
21 changes: 12 additions & 9 deletions lib/test_prof/recipes/rspec/let_it_be.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ def let_it_be(identifier, **options, &block)
instance_variable_get(:"#{PREFIX}#{identifier}")
end

if self.instance_methods.include?(:"let_it_be_#{identifier}")
error_msg = "let_it_be(:#{identifier}) was redefined in nested group"

if LetItBe.config.report_duplicates == :warn
::RSpec.warn_with(error_msg)
else
raise DuplicationError, error_msg
end
end

LetItBe.module_for(self).module_eval do
define_method(identifier) do
# Trying to detect the context
Expand All @@ -145,16 +155,9 @@ def let_it_be(identifier, **options, &block)
super()
end
end
end

unless self.instance_method(identifier).super_method.nil?
error_msg = "let_it_be(:#{identifier}) was redefined in nested group"

if LetItBe.config.report_duplicates == :warn
::RSpec.warn_with(error_msg)
else
raise DuplicationError, error_msg
end
# Used to to track method overrides
define_method("let_it_be_#{identifier}") {}
end

let(identifier, &let_accessor)
Expand Down
180 changes: 112 additions & 68 deletions spec/integrations/fixtures/rspec/let_it_be_nested_fixture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,130 +6,174 @@
require "test_prof/recipes/rspec/let_it_be"

RSpec.describe "Overriding detection", :transactional do
context "when report_duplicates was set as :raise and let_it_be override in nested context" do
context "when on same nested level" do
it "raises a duplication error" do
expect do
TestProf::LetItBe.configure do |config|
config.report_duplicates = :raise
end
context "when report_duplicates was set as :raise" do
context "when let_it_be redefined" do
context "when on same nested level" do
it "raises a duplication error" do
expect do
TestProf::LetItBe.configure do |config|
config.report_duplicates = :raise
end

RSpec.describe "let_it_be on same nested level" do
include TestProf::FactoryBot::Syntax::Methods
RSpec.describe "let_it_be on same nested level" do
include TestProf::FactoryBot::Syntax::Methods

let_it_be(:user) { create(:user) }
let_it_be(:user) { create(:user) }
end
end.to raise_error(TestProf::LetItBe::DuplicationError)
let_it_be(:user) { create(:user) }
let_it_be(:user) { create(:user) }
end
end.to raise_error(TestProf::LetItBe::DuplicationError)
end
end
end

context "when nested level is 2" do
it "raises a duplication error" do
expect do
TestProf::LetItBe.configure do |config|
config.report_duplicates = :raise
end
context "when nested level is 2" do
it "raises a duplication error" do
expect do
TestProf::LetItBe.configure do |config|
config.report_duplicates = :raise
end

RSpec.describe "let_it_be in nested context" do
include TestProf::FactoryBot::Syntax::Methods
RSpec.describe "let_it_be in nested context" do
include TestProf::FactoryBot::Syntax::Methods

let_it_be(:user) { create(:user) }
let_it_be(:user) { create(:user) }

context "nested context level 2" do
let_it_be(:user) { create(:user) }
end
end
end.to raise_error(TestProf::LetItBe::DuplicationError)
end
end

context "when nested level is 3" do
it "raises a duplication error" do
expect do
TestProf::LetItBe.configure do |config|
config.report_duplicates = :raise
end

RSpec.describe "let_it_be in nested context" do
include TestProf::FactoryBot::Syntax::Methods

context "nested context level 2" do
let_it_be(:user) { create(:user) }

context "nested context level 2" do
context "nested context level 3" do
let_it_be(:user) { create(:user) }
end
end
end
end
end.to raise_error(TestProf::LetItBe::DuplicationError)
end.to raise_error(TestProf::LetItBe::DuplicationError)
end
end
end

context "when nested level is 3" do
it "raises a duplication error" do
context "when defined let and let_it_be" do
it "does not raise a duplication error" do
expect do
TestProf::LetItBe.configure do |config|
config.report_duplicates = :raise
end

RSpec.describe "let_it_be in nested context" do
RSpec.describe "let_it_be and let" do
include TestProf::FactoryBot::Syntax::Methods

let_it_be(:user) { create(:user) }
let(:user) { create(:user) }

context "nested context level 2" do
context "nested context 3" do
let_it_be(:user) { create(:user) }
end
let_it_be(:user) { create(:user) }
end
end
end.to raise_error(TestProf::LetItBe::DuplicationError)
end.not_to raise_error
end
end
end

context "when report_duplicates was set as :warn and let_it_be override in nested context" do
context "when report_duplicates was set as :warn" do
let(:warning_msg) { "let_it_be(:user) was redefined in nested group" }

before do
allow(::RSpec).to receive(:warn_with).with(warning_msg)
end

context "when on same nested level" do
it "warns a duplication error" do
RSpec.describe "let_it_be on same nested level" do
include TestProf::FactoryBot::Syntax::Methods
context "when let_it_be redefined" do
context "when on same nested level" do
it "warns a duplication message" do
RSpec.describe "let_it_be on same nested level" do
include TestProf::FactoryBot::Syntax::Methods

TestProf::LetItBe.configure do |config|
config.report_duplicates = :warn
end
TestProf::LetItBe.configure do |config|
config.report_duplicates = :warn
end

let_it_be(:user) { create(:user) }
let_it_be(:user) { create(:user) }
end.run
let_it_be(:user) { create(:user) }
let_it_be(:user) { create(:user) }
end.run

expect(::RSpec).to have_received(:warn_with).with(warning_msg).once
expect(::RSpec).to have_received(:warn_with).with(warning_msg).once
end
end
end

context "when nested level is 2" do
it "warns a duplication warning message" do
RSpec.describe "let_it_be in nested context" do
include TestProf::FactoryBot::Syntax::Methods
context "when nested level is 2" do
it "warns a duplication message" do
RSpec.describe "let_it_be in nested context" do
include TestProf::FactoryBot::Syntax::Methods

TestProf::LetItBe.configure do |config|
config.report_duplicates = :warn
end
TestProf::LetItBe.configure do |config|
config.report_duplicates = :warn
end

let_it_be(:user) { create(:user) }
let_it_be(:user) { create(:user) }

context "nested context" do
let_it_be(:user) { create(:user) }
end
end.run

expect(::RSpec).to have_received(:warn_with).with(warning_msg).once
end
end

context "when nested level is 3" do
it "warns a duplication message" do
RSpec.describe "let_it_be in nested context" do
include TestProf::FactoryBot::Syntax::Methods

TestProf::LetItBe.configure do |config|
config.report_duplicates = :warn
end

context "nested context" do
let_it_be(:user) { create(:user) }
end
end.run

expect(::RSpec).to have_received(:warn_with).with(warning_msg).once
context "nested context level 2" do
context "nested context level 3" do
let_it_be(:user) { create(:user) }
end
end
end.run

expect(::RSpec).to have_received(:warn_with).with(warning_msg).once
end
end
end

context "when nested level is 3" do
it "warns a duplication error" do
RSpec.describe "let_it_be in nested context" do
context "when defined let and let_it_be" do
it "does not warn a duplication message" do
RSpec.describe "let_it_be and let" do
include TestProf::FactoryBot::Syntax::Methods

TestProf::LetItBe.configure do |config|
config.report_duplicates = :warn
config.report_duplicates = :raise
end

let_it_be(:user) { create(:user) }
let(:user) { create(:user) }

context "nested context level 2" do
context "nested context 3" do
let_it_be(:user) { create(:user) }
end
let_it_be(:user) { create(:user) }
end
end.run

expect(::RSpec).to have_received(:warn_with).with(warning_msg).once
expect(::RSpec).not_to have_received(:warn_with)
end
end
end
Expand Down

0 comments on commit 5f0895b

Please sign in to comment.