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

Add find_or_create_by Method #1681

Open
hatsu38 opened this issue Jul 20, 2024 · 0 comments
Open

Add find_or_create_by Method #1681

hatsu38 opened this issue Jul 20, 2024 · 0 comments
Labels

Comments

@hatsu38
Copy link

hatsu38 commented Jul 20, 2024

Problem this feature will solve

When setting up test data with FactoryBot, it can be cumbersome and repetitive to check if a record exists before creating it.
For instance, in our current test setup, we have to manually use find_by followed by create if the record is not found.

RSpec.describe User do 
  before(:all) { Fixtures.setup_basics }
  
  it do
    # aa
  end
  
  it do
    # aa
  end
end


module Fixtures
  def self.setup_basics
    setup_normal_plan
    setup_high_plan
  end

  def self.normal_plan
      a_token = FactoryBot.create(:token :free)
      b_token = FactoryBot.create(:token, :normal)
     # etc...
  end

  def self.high_plan
    a_token = Token.find_by(name: "free") || FactoryBot.create(:token :free)
    b_token = Token.find_by(name: "normal") || FactoryBot.create(:token :normal)
  end
end

Desired solution

  def self.high_plan
    a_token = Token.find_or_create_by(:token :free)
    b_token = Token.find_or_create_by(:token :normal)
  end

Alternatives considered

  def self.high_plan
    a_token = Token.find_by(name: "free") || FactoryBot.create(:token :free)
    b_token = Token.find_by(name: "normal") || FactoryBot.create(:token :normal)
  end

Additional context

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

No branches or pull requests

1 participant