Ruby on rails 运行rspec时出现未初始化常量名错误,但在rails控制台中工作正常
在使用FactoryGirl运行rspec时,我遇到了“nameError:uninitialized constant”错误,但当我在控制台内通过运行rails c test调用FactoryGirl.build时,它会正确识别FactoryGirl 以下是rspec的输出:Ruby on rails 运行rspec时出现未初始化常量名错误,但在rails控制台中工作正常,ruby-on-rails,rspec,factory-bot,Ruby On Rails,Rspec,Factory Bot,在使用FactoryGirl运行rspec时,我遇到了“nameError:uninitialized constant”错误,但当我在控制台内通过运行rails c test调用FactoryGirl.build时,它会正确识别FactoryGirl 以下是rspec的输出: Failures: 1) create user test Failure/Error: FactoryGirl.build(:user) NameError: uninitialized constant
Failures:
1) create user test
Failure/Error: FactoryGirl.build(:user)
NameError:
uninitialized constant User
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:26:in `build_class'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:37:in `run'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
# /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/model/user_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.001 seconds (files took 0.31845 seconds to load)
/spec/rails\u helper.rb:
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before do
FactoryGirl.find_definitions
end
...
end
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
...
end
require 'spec_helper'
# Create factory de user
describe "create user" do
it "test" do
u = FactoryGirl.build(:user)
end
end
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
gem 'capybara'
gem 'factory_girl_rails'
end
/spec/model/user\u spec.rb:
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before do
FactoryGirl.find_definitions
end
...
end
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
...
end
require 'spec_helper'
# Create factory de user
describe "create user" do
it "test" do
u = FactoryGirl.build(:user)
end
end
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
gem 'capybara'
gem 'factory_girl_rails'
end
/Gemfile:
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.before do
FactoryGirl.find_definitions
end
...
end
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'factory_girl_rails'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
...
end
require 'spec_helper'
# Create factory de user
describe "create user" do
it "test" do
u = FactoryGirl.build(:user)
end
end
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
gem 'capybara'
gem 'factory_girl_rails'
end
您是否有可能使用rspec 3? 从rspec3开始,您需要
require'rails\u helper'
而不是spec\u helper
请参见您是否碰巧使用了rspec 3? 从rspec3开始,您需要
require'rails\u helper'
而不是spec\u helper
请参见以解决
名称错误
:
- 您需要将FactoryGirl的配置从
移动到/spec/spec\u helper.rb
/spec/rails\u helper.rb
- 您需要在
中将/spec/model/user_spec.rb
替换为spec\u helper
rails helper
请试一试。要解决
名称错误
:
- 您需要将FactoryGirl的配置从
移动到/spec/spec\u helper.rb
/spec/rails\u helper.rb
- 您需要在
中将/spec/model/user_spec.rb
替换为spec\u helper
rails helper
请试一试。是的,我正在使用rspec 3.4。我刚刚测试了你所说的,并且通过一些小的修改,一切都正常了。我需要从rails\u helper.rb中删除行“require'factory\u girl\u rails'”和“config.include FactoryGirl::Syntax::Methods”,还必须从spec\u helper.rbIn/spec/model/user\u spec.rb中删除“config.before do FactoryGirl.find\u definitions end”,尝试使用
require'rails\u helper'
是的,我正在使用rspec 3.4。我刚刚测试了你所说的,并且通过一些小的修改,一切都正常了。我需要从rails\u helper.rb中删除行“require'factory\u girl\u rails'”和“config.include FactoryGirl::Syntax::Methods”,还必须从spec\u helper.rbIn/spec/model/user\u spec.rb中删除“config.before do FactoryGirl.find\u definitions end”,尝试使用require'rails\u helper'