이 테스트에서 내가 잘못하고있는 아이디어가 있습니까? 공장의 연관 region
가 구축되면 지역레일 FactoryGirl 중복 키 값이 모델의 기본 키 특성을 변경할 때 고유 제한을 위반합니다.
class Region < ApplicationRecord
self.primary_key = "lid"
has_many :reps
end
이 클래스 감안할 때
는require "rails_helper"
describe "Rep", type: :feature do
scenario "has a Manager and a Region" do
rep = FactoryGirl.create(:rep)
expect(rep.boss.email).to eq("[email protected]")
expect(rep.region.name).to eq("NAT")
end
end
나는 열 이제 lid
FactoryGirl.define do
factory :rep do
lid 1
email '[email protected]'
association :boss, strategy: :build
association :region, factory: :region, strategy: :build
end
factory :boss, class: Rep do
lid 2
email '[email protected]'
manager true
association :region, factory: :region, strategy: :build
end
end
를 참조하는 지역의 기본 키를 변경 한 이 지역에 대한 유일성 검증을 야기하고 있습니다.
다른 시스템에서 가져온 legacy ids (lid)라는이 ID를 사용하고이를 기본 키로 사용합니다 (데이터베이스 마이그레이션이이를 처리하도록 설정되어 있습니다).
내가 너무 표준 'ID'필드를 가지고이 응용 프로그램에 고유 한 다른 연결을 구축 할 수 있습니다
FactoryGirl.define do
factory :region do
lid 10
name 'NAT'
end
end
내가 모두 rep
과 boss
때 너무 같은 region
에 속하고하는 것입니다 원하는 모든 I boss
및 region
연관이 제공되는 rep
팩토리를 빌드하십시오. 혼란 : 지금까지 난 그냥이 지역 협회를 주석 할 수 있도록 belongs_to optional
을 해제 region_id null false flag
을 켜고 만드는 멀리 입수했습니다
rspec spec spec/features/reps/have_managers.rb
F
Failures:
1) Reps belong to a manager
Failure/Error: rep = FactoryGirl.create(:rep)
ActiveRecord::RecordNotUnique:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_regions_on_lid"
DETAIL: Key (lid)=(10) already exists.
: INSERT INTO "regions" ("lid", "name", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "lid"
# ./spec/features/reps/have_managers.rb:6:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# PG::UniqueViolation:
# ERROR: duplicate key value violates unique constraint "index_regions_on_lid"
# DETAIL: Key (lid)=(10) already exists.
# ./spec/features/reps/have_managers.rb:6:in `block (2 levels) in <top (required)>'
는 잘하면 내가 고려하고 있지 않다 바보 뭔가있다 그것을 지금 무시하십시오
당신은 당신의 공장에서 동적으로 고유의'lid's을 생성해야 . – chumakoff