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

Warn on let_it_be duplications #291

Open
palkan opened this issue May 4, 2024 · 0 comments · May be fixed by #293
Open

Warn on let_it_be duplications #291

palkan opened this issue May 4, 2024 · 0 comments · May be fixed by #293
Labels
hackday RailsConf Hack Day challenges

Comments

@palkan
Copy link
Collaborator

palkan commented May 4, 2024

Context

Although we suggest using let_it_be instead of let!, there is one important difference: you can override let! definition with the same or nested context, so only the latter one is called; let_it_be records could be overridden, but still created. For example:

context "A" do
  let!(:user) { create(:user, name: "a") } 
  let_it_be(:post) { create(:post, title: "A") }

  specify { expect(User.all.pluck(:name)).to eq ["a"] }
  specify { expect(Post.all.pluck(:title)).to eq ["A"] }

  context "B" do
    let!(:user) { create(:user, name: "b") }
    let_it_be(:post) { create(:post, title: "B") }

    specify { expect(User.all.pluck(:name)).to eq ["b"] }
    specify { expect(Post.all.pluck(:title)).to eq ["B"] } # fails, because there are two posts
  end
end

The situation becomes even more trickier when you have multiple let_it_be records and dependencies between them.

TODO

Add a configuration option to warn or raise an exception whenever a let_it_be definition is redefined:

TestProf::LetItBe.configure do |config|
  config.report_duplicates = :warn # Kernel.warn
  config.report_duplicates = :raise # Kernel.raise
end
@palkan palkan added the hackday RailsConf Hack Day challenges label May 4, 2024
@lHydra lHydra linked a pull request May 17, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hackday RailsConf Hack Day challenges
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant