ロケーションはアプリケーションの所有者とユーザーに属しているため、この事実に基づいて構築したいと考えています。したがって、私の工場では次のようになります。
FactoryGirl.define do
factory :user do
username 'user1'
email 'user@example.com'
timezone 'Eastern Time (US & Canada)'
password 'testing'
end
factory :owner do
name 'Owner One'
user
end
factory :location do
name 'Location One'
about 'About this location'
website 'http://www.locationone.com/'
phone_number '12 323-4234'
street_address 'Shibuya, Tokyo, Japan'
owner
user
end
end
私のspec/models/location_spec.rbよりも
describe Location do
before(:each) do
@location = FactoryGirl.build(:location)
end
end
私のモデルのlocation.rbより
class Location < ActiveRecord::Base
attr_accessible :name, :about. :website, phone_number,
:street_address, owner_id
belongs_to :user
belongs_to :owner
end
注:owner_id
選択可能ですのでご利用いただけます。
このすべてを使用すると、次のようにテストの失敗が返されます。
Failure/Error: @location = FactoryGirl.build(:location)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken, Email has already been taken, Username
これは、所有者が最初にユーザーを作成すべきではないときに最初にユーザーを作成し、場所が同じユーザーを作成するためだと思います。では、どうすればこれを回避できますか?