dimanche 11 février 2018

Dependent Rails model in FactoryGirl

I'm refactoring specs to support a migration of a string to a foreign key in a new table.

Questions had a string, type, which is now a new table: question_types. The factory is configured to use a random string for the value of type. How can I update it so that there are 5 valid types that it can be assigned as needed?

What I tried to do was update the question factory with

factory.define do

  before(:create) do 
    FactoryGirl.create(:question_types) if QuestionType.all.empty?
  end

# ...more factory

end

But when I run the specs, the value of question type is always nil.

Aucun commentaire:

Enregistrer un commentaire