I'm using FactoryBot
to create fake data for my Rspec tests. My factory for the users is as follows:
FactoryBot.define do
factory :user do
sequence(:name) { |n| "User#{n}" }
sequence(:email) { |n| "user#{n}@email.com" }
end
end
Creating a user creates a client automatically in my User
model as an after action:
def initialize_client
self.client = self.build_client
self.client.setup_stripe
self.save
end
And I have a factory for client as:
FactoryBot.define do
factory :client do
user
end
end
I created an Rspec file to test if the client
gets build properly on creating a User as:
describe User, type: :model do
user = FactoryBot.create(:user)
end
But this raises the error:
raise_record_invalid': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)
Even though running FactoryBot.create(:user)
creates the Client and User as accepted. I'm not sure what I need to change at this point
RAILS_ENV
and it works. This is weird though, I'll accept it as the answer if you can add that. Do you mind explaining why I had to this though? Ty – anonn023432