由于位置属于我的应用程序中的所有者和用户,我想根据这个事实来构建它。所以在我的工厂里是这样的:
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
比我有我的规范/模型/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
我希望这是因为所有者在不应该创建用户时首先创建用户,然后该位置创建相同的用户。那么我该如何解决呢?