How can I define a method on a FactoryGirl factory?

Viewed 4661

How can I define a normal method for use in one of my FactoryGirl factories? For instance:

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

Doing it this way does not call silly_horse_name at all; if it's redefined to raise 'hey!', nothing happens.

I'm using FactoryGirl 2.5.2.

3 Answers
Related