FactoryGirl creates objects in the development environment

When I launch the rail console in development, I see FactoryGirl objects creating objects. It’s clear that I am doing it wrong, but how to do it right? This code makes my tests work ...

# tests/factories/board.rb FactoryGirl.define do factory :word do sequence(:text) { |n| "FAKETEXT#{n}" } end factory :board do trait :has_words do words [ FactoryGirl.create(:word, id: "514b81cae14cfa78f335e250"), FactoryGirl.create(:word, id: "514b81cae14cfa7917e443f0"), FactoryGirl.create(:word, id: "514b81cae14cfa79182407a2"), FactoryGirl.create(:word, id: "514b81cae14cfa78f581c534") ] end end end 

Please note that my config directory does not mention factory anything in any file, so any download happens automatically with a gem. The relevant part of my Gemfile reads:

 # Stuff not to use in production group :development, :test do # Command-line debugger for development gem "debugger" # for unit testing - replace fixtures gem "factory_girl_rails" end 

So I could just take a factory girl from the development environment. But I think the fact that these records are created before using the factory is a sign that I wrote my factory incorrectly. But if you tell me that the factory is spelled correctly, I will just do it.

+4
source share
4 answers

There was the same problem. There are two ways to fix this.

1. Use FactoryGirl syntax to reference Factory in Factory.

Replace FacotryGirl.create(:my_factory) with factory: :my_factory

More on this, https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations

2. factory_girl :require => false in the Gemfile

This forces factories to generate objects at boot time.

 group :development, :test do gem 'factory_girl_rails' end 

Why? At boot time, the Rails Bundler requires all the gems in the development group, and FactoryGirl seems to require all of its Factory files. The requirement that factories evaluate Ruby code, and thus FactoryGirl.create(:my_factory) called.

It can be fixed

 # Gemfile group :development, :test do gem 'factory_girl_rails', :require => false end 

Just make sure factory_girl is required manually in the test environment , eg

 # spec_helper require 'factory_girl' 
+7
source

for each factory or model, you must put in a different file

 spec/factories/word_factory.rb spec/factories/board_factory.rb 

So, the contents of each factory, you can do something similar to this:

 FactoryGirl.define do factory :board do word special_id end end 

when in your test folder e.g. models / board_spec.rb

you can create your own object

 let(:word) { FactoryGirl.create(:word) let(:board) { FactoryGirl.create(:board, word: word) } 

Not sure, this is what you need, correct me if I'm wrong

0
source

Assuming that you need words created when you call factory, and not when Rails starts, you need to put an array of words inside the block, namely:

 trait :has_words do words do [ FactoryGirl.create(:word, id: "514b81cae14cfa78f335e250"), FactoryGirl.create(:word, id: "514b81cae14cfa7917e443f0"), FactoryGirl.create(:word, id: "514b81cae14cfa79182407a2"), FactoryGirl.create(:word, id: "514b81cae14cfa78f581c534") ] end end 
0
source

You just need to move the factory girl from the development environment.

I had the same problem, so I just did

 group :test do gem 'faker' gem 'factory_girl_rails' end 

And it works like a charm.

I do not use these gems at all in development, so it can simply be determined in the test.

0
source

All Articles