ruby – 如何在FactoryGirl工厂中定义方法?
发布时间:2020-12-17 04:39:27 所属栏目:百科 来源:网络整理
导读:如何定义在FactoryGirl工厂中使用的常规方法?例如: FactoryGirl.define do def silly_horse_name verbs = %w[brunches dribbles haggles meddles] nouns = %w[landmines hamlets vandals piglets] "#{verbs.sample} with #{nouns.sample}".titleize end fa
如何定义在FactoryGirl工厂中使用的常规方法?例如:
FactoryGirl.define do def silly_horse_name verbs = %w[brunches dribbles haggles meddles] nouns = %w[landmines hamlets vandals piglets] "#{verbs.sample} with #{nouns.sample}".titleize end factory :racehorse do name { silly_horse_name } # eg,"Brunches with Landmines" after_build do |horse,evaluator| puts "oh boy,I built #{silly_horse_name}!" end end end 这样做根本不会调用silly_horse_name;如果将它重新定义为“嘿!”,则没有任何反应. 我正在使用FactoryGirl 2.5.2. 解决方法
我发现没有污染全局命名空间的最佳解决方案是在模块中定义它.例如.,
module FactoryHelpers extend self def silly_horse_name ... end end FactoryGirl.define do factory :racehorse do name { silly_horse_name } end end Source (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |