作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
工厂女孩/机械师的工厂中是否有一些配置强制它在测试用例期间只创建一次具有相同工厂名称的对象并始终返回相同的实例?我知道,我可以这样做:
def singleton name
@@singletons ||= {}
@@singletons[name] ||= Factory name
end
...
Factory.define :my_model do |m|
m.singleton_model { singleton :singleton_model }
end
最佳答案
您可以在工厂中使用 initialize_with
宏并检查对象是否已经存在,然后不要再次创建它。当所述工厂被关联引用时,这也有效:
FactoryGirl.define do
factory :league, :aliases => [:euro_cup] do
id 1
name "European Championship"
owner "UEFA"
initialize_with { League.find_or_create_by_id(id)}
end
end
关于factory_girl/机械师中的单例工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4317848/
我对我的模型进行了 validates_uniqueness_of 验证: #SwimMeetRelayEvent.rb validates_uniqueness_of :event_number_d
我正在尝试为相对复杂的模型建立工厂。 我有一个Pressroom模型,属于Source,Source有很多Pressroom。创建 Source 时,如果 pressrooms 为空,则会在 afte
我正在与 factory_girl 一起工作,但正在研究机械师 gem。你能告诉我 - 迁移到机械师的利弊是什么?您是否比较过这些库? 最佳答案 Machinist实际上深受 factory_girl
我是一名优秀的程序员,十分优秀!