"Name has already been taken" 在 RSpec/FactoryGirl 中具有多个关联



我正在尝试运行一个非常基本的规范测试,但它失败并显示错误"名称已被占用"。

更新属于具有许多角色的用户

用户模型

# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string           default(""), not null
#
FactoryGirl.define do
  factory :user_engineer, class: User do
    id 1
    email 'someone@somewhere.com'
    roles {[FactoryGirl.create(:engineer)]}
  end
end

榜样

# == Schema Information
#
# Table name: roles
#
#  id          :integer          not null, primary key
#  name        :string
#  description :text
#
FactoryGirl.define do
  factory :engineer, class: Role do
    id 3
    name 'Engineer'
    description 'He is the chosen one'
  end
end

更新模型

# == Schema Information
#
# Table name: updates
#
#  id            :integer          not null, primary key
#  content       :text
#  user_id       :integer
#  ticket_id :integer
#
FactoryGirl.define do
  factory :update do
    content "This is a test update"
    association :user, factory: :user_engineer
  end
end

update_spec.rb

require 'rails_helper'
RSpec.describe Update, type: :model do
  let(:update){ FactoryGirl.create :update }
  it { expect(update).to be_valid }
end

这是错误:

Update
  example at ./spec/models/update_spec.rb:19 (FAILED - 1)
Failures:
  1) Update 
     Failure/Error: roles {[FactoryGirl.create(:engineer)]}
     ActiveRecord::RecordInvalid:
       Validation failed: Name has already been taken

我怎样才能通过测试?!

编辑:通过添加我建议的序列行,我在运行RAILS_ENV=test rake db:drop后出现以下错误:

1) Update 
     Failure/Error: roles {[FactoryGirl.create(:engineer)]}
     ActiveRecord::RecordNotUnique:
       PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "roles_pkey"
       DETAIL:  Key (id)=(3) already exists.
       : INSERT INTO "roles" ("id", "name", "description", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"

从您的错误中可以清楚地看出您对name属性进行了 uniq 验证,因此您应该使用sequence技术。

FactoryGirl.define do
  factory :engineer, class: Role do
    id 3
    sequence(:name) { |n| "Engineer-#{n}" }
    description 'He is the chosen one'
  end
end

为用户模型尝试以下代码

FactoryGirl.define do
  factory :user_engineer, class: User do
    id 1
    email 'someone@somewhere.com'
    roles {[FactoryGirl.create(:engineer, name: "new_engineer")]}
  end
end

由于 name 属性上存在 uniq 约束,我认为您的测试数据库中已经有一条工程师记录,该记录是在您第一次运行测试用例时添加的,因此最好在运行测试用例之前或之后清除测试数据库。

将以下代码块放入 spec/rails_helper.rb 文件中。

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end

最新更新