I can't seem to add “config.include FactoryGirl::Syntax::Methods” to my rspec config block in spec_helper.rb

你离开我真会死。 提交于 2019-12-06 20:52:27

问题


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.



来源:https://stackoverflow.com/questions/25648456/i-cant-seem-to-add-config-include-factorygirlsyntaxmethods-to-my-rspec-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!