Ai đó có thể cho tôi biết liệu tôi có đang thiết lập sai cách không?Cách thiết lập nhà máy tại FactoryGirl với liên kết has_many
tôi có các mô hình sau đó có các hiệp hội 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
Tôi đang sử dụng Rails 3.1.rc4, FactoryGirl 2.0.2, 1.1.0 factory_girl_rails, và rspec. Dưới đây là rspec rspec kiểm tra của tôi cơ bản sự tỉnh táo cho nhà máy :listing
:
it "creates a valid listing from factory" do
Factory(:listing).should be_valid
end
Đây là Nhà máy (: niêm yết)
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
Các :listing_feature
và :feature
nhà máy là tương tự như thiết lập.
Nếu dòng association :features
được nhận xét, thì tất cả các thử nghiệm của tôi đều được chuyển.
Khi nó là
association :features, :factory => :feature
được thông báo lỗi là undefined method 'each' for #<Feature>
mà tôi nghĩ có ý nghĩa với tôi bởi vì vì listing.features
lợi nhuận một mảng. Vì vậy, tôi đã thay đổi nó để
association :features, [:factory => :feature]
và lỗi tôi có được bây giờ là ArgumentError: Not registered: features
Là nó chỉ không hợp lý để được tạo nhà máy đối tượng theo cách này, hoặc những gì tôi bị mất? Cảm ơn rất nhiều cho bất kỳ và tất cả các đầu vào!
bạn có sử dụng liên kết: tính năng, [: factory =>: feature] không? – davidtingsu