我有 3 个模型:
Article:
has_many photos
Photo:
belongs_to article
belongs_to photoType
PhotoType:
has_many articles
还有一家工厂:
FactoryGirl.define do
factory :article do
title 'The Batcave'
content '5 Smith Street'
after_build do |article|
article.photos << FactoryGirl.build(:photo, :article => article)
article.photos << FactoryGirl.build(:photo, :article => article)
end
end
end
在文章模型中,我有一个方法 get_photo(type) 查询数据库并根据类型返回一个正确的照片对象。
我的问题是如何在我的工厂中存根这种方法。现在 get_photo 总是返回 nil。
存根 article.get_photo(:big) 应该返回 article.photos[0]