有人能告訴我,如果我只是以錯誤的方式進行安裝?如何在has_many協會的FactoryGirl中設置工廠
我有has_many.through協會以下型號:
class Listing < ActiveRecord::Base
attr_accessible ...
has_many :listing_features
has_many :features, :through => :listing_features
validates_presence_of ...
...
end
class Feature < ActiveRecord::Base
attr_accessible ...
validates_presence_of ...
validates_uniqueness_of ...
has_many :listing_features
has_many :listings, :through => :listing_features
end
class ListingFeature < ActiveRecord::Base
attr_accessible :feature_id, :listing_id
belongs_to :feature
belongs_to :listing
end
我使用Rails 3.1.rc4,FactoryGirl 2.0.2,1.1.0 factory_girl_rails,和RSpec。下面是:listing
工廠我的基本rspec的rspec的健全性檢查:
it "creates a valid listing from factory" do
Factory(:listing).should be_valid
end
這裏是廠(:上市)
FactoryGirl.define do
factory :listing do
headline 'headline'
home_desc 'this is the home description'
association :user, :factory => :user
association :layout, :factory => :layout
association :features, :factory => :feature
end
end
的:listing_feature
和:feature
工廠也同樣設置。
如果association :features
行被註釋掉,那麼我所有的測試都會通過。
當它是
association :features, :factory => :feature
錯誤消息是 undefined method 'each' for #<Feature>
而且我認爲他對我有意義,因爲,因爲listing.features
返回數組。所以,我把它改成
association :features, [:factory => :feature]
,我現在得到的錯誤是ArgumentError: Not registered: features
難道只是不懂事的是生成工廠對象這樣,還是我缺少什麼?非常感謝任何和所有的輸入!
你最終使用關聯:features,[:factory =>:feature]? – davidtingsu