最佳答案
谁能告诉我,我是不是走错了方向?
我有以下模型,它们有 _ 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、 Factory _ girl _ ails 1.1.0和 rspec。下面是我对 :listing
工厂的基本规格检查:
it "creates a valid listing from factory" do
Factory(:listing).should be_valid
end
这里是 Factory (: 列表)
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
错误消息是
因为 listing.features
返回一个数组,所以我认为 undefined method 'each' for #<Feature>
对我来说是有意义的。所以我改成了
association :features, [:factory => :feature]
我现在得到的错误是 ArgumentError: Not registered: features
这样生成工厂对象是不明智的吗,还是我错过了什么?非常感谢所有的投入!