由於位置屬於我的應用程序中的所有者和用戶,因此我希望基於這個事實構建它。所以在我的工廠是這樣的:Rspec:如何讓工廠有兩個協會?
FactoryGirl.define do
factory :user do
username 'user1'
email '[email protected]'
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
比我有我的規格/型號/ 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
我希望這是因爲所有者首先創建用戶時,它不應該,比位置創建相同的用戶:
有了這一切,雖然這將返回我的測試失敗。那麼我該如何解決這個問題?
您能否提供位置模型的完整定義? –
@MattRogers好的,我現在就做了。 – LearningRoR
你想要什麼樣的關聯?你想讓'Location'屬於一個用戶和一個擁有者嗎? –