【发布时间】:2018-10-29 20:58:33
【问题描述】:
我正在使用 FactoryBot 和 Faker 进行测试,看起来 Faker 正在生成相同的名称:
class Profile < ApplicationRecord
belongs_to :user
validates_presence_of :first_name, :last_name, :nickname
validates :nickname, uniqueness: { case_sensitive: false }
end
FactoryBot.define do
factory :user do
sequence(:email) { |n| "user#{n}@example.org" }
password "123456"
trait :with_profile do
profile
end
end
end
FactoryBot.define do
factory :profile do
first_name Faker::Name.unique.first_name
last_name Faker::Name.unique.last_name
nickname { "#{first_name}_#{last_name}".downcase }
user
end
end
RSpec.feature "Friendships", type: :feature do
scenario "User can accept a pending friendship request" do
@tom = create(:user, :with_profile)
@jerry = create(:user, :with_profile)
#other stuff
end
end
即使我使用独特的方法,我也会收到错误
ActiveRecord::RecordInvalid: Validation failed: Nickname has already been taken`.
有什么线索吗?
【问题讨论】:
标签: ruby-on-rails ruby rspec factory-bot faker