I can't seem to add “config.include FactoryGir

2019-04-21 06:42发布

问题:

If I add:

config.include FactoryGirl::Syntax::Methods  

under

RSpec.configure do |config|  

and run rspec, I see this error:

/Users/perry_mac/rails_projects/mymri/spec/spec_helper.rb:21:in `block in ': uninitialized constant FactoryGirl (NameError)

my gemfile.lock can be seen in this pastebin
my gemfile can be seen in this pastebin

If I omit the Rspec.comfigure statment, my tests all run fine. I'd like to make use of the abbreviated syntax, but am not sure what I am doing wrong here.

回答1:

You must add this string in file 'spec/RAILS_helper.rb' not in 'spec_helper.rb'



回答2:

Got it.

This link showed me the way.

The required addition should be made in spec/support/factory_girl.rb and it should look like this:

# RSpec
# spec/support/factory_girl.rb
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end


回答3:

Also make sure to require 'factory_bot' in spec/support/factory_bot.rb

That's what ended up being the issue for me.



回答4:

Make sure to include require 'support/factory_girl' in spec/rails_helper.rb after require 'rspec/rails'.

I was getting this error after putting it right after require 'spec_helper'.



回答5:

I think it's worth mentioning here that, going forward, if using FactoryGirl, you will receive a deprecation message:

The factory_girl gem has been deprecated and has been replaced by factory_bot. Please switch to factory_bot as soon as possible.

Just a note for developers in the future that are trying to use FactoryGirl.