Ruby on Rails 4中,为何rspec运行时FactoryGirl未初始化,而在控制台却正常工作?

2026-04-11 16:221阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计296个文字,预计阅读时间需要2分钟。

Ruby on Rails 4中,为何rspec运行时FactoryGirl未初始化,而在控制台却正常工作?

这是我的第一次使用rspec。运行rspec时,我遇到了未初始化的FactoryGirl问题。我通过运行rails c test在控制台尝试了,FactoryGirl能被正确识别。这是rspec的输出:Failures: 1) basic API gives authentication to

这是我第一次使用rspec.运行rspec时,我得到了未初始化的常量FactoryGirl.我通过运行rails c test在控制台中尝试了,它正确识别FactoryGirl.

这是rspec的输出:

Failures: 1) basic API gives authentication token from username and password Failure/Error: user = FactoryGirl.create(:user, last_name: last_name) NameError: uninitialized constant FactoryGirl # ./spec/requests/api/v1/api_spec.rb:4:in `block (2 levels) in <top (required)>' Finished in 0.00069 seconds 1 example, 1 failure Failed examples: rspec ./spec/requests/api/v1/api_spec.rb:2 # basic API gives authentication token from username and password

这是我的spec / spec_helper.rb:

Ruby on Rails 4中,为何rspec运行时FactoryGirl未初始化,而在控制台却正常工作?

# This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'factory_girl_rails' FactoryGirl.find_definitions Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = false config.order = "random" end

我不太确定在哪里需要’factory_girl_rails和FactoryGirl.find_definitions,因为在指南中它只是说“所有你需要做的就是运行FactoryGirl.find_definitions”

我忘了在我的spec文件中要求’spec_helper’.

本文共计296个文字,预计阅读时间需要2分钟。

Ruby on Rails 4中,为何rspec运行时FactoryGirl未初始化,而在控制台却正常工作?

这是我的第一次使用rspec。运行rspec时,我遇到了未初始化的FactoryGirl问题。我通过运行rails c test在控制台尝试了,FactoryGirl能被正确识别。这是rspec的输出:Failures: 1) basic API gives authentication to

这是我第一次使用rspec.运行rspec时,我得到了未初始化的常量FactoryGirl.我通过运行rails c test在控制台中尝试了,它正确识别FactoryGirl.

这是rspec的输出:

Failures: 1) basic API gives authentication token from username and password Failure/Error: user = FactoryGirl.create(:user, last_name: last_name) NameError: uninitialized constant FactoryGirl # ./spec/requests/api/v1/api_spec.rb:4:in `block (2 levels) in <top (required)>' Finished in 0.00069 seconds 1 example, 1 failure Failed examples: rspec ./spec/requests/api/v1/api_spec.rb:2 # basic API gives authentication token from username and password

这是我的spec / spec_helper.rb:

Ruby on Rails 4中,为何rspec运行时FactoryGirl未初始化,而在控制台却正常工作?

# This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'factory_girl_rails' FactoryGirl.find_definitions Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_base_class_for_anonymous_controllers = false config.order = "random" end

我不太确定在哪里需要’factory_girl_rails和FactoryGirl.find_definitions,因为在指南中它只是说“所有你需要做的就是运行FactoryGirl.find_definitions”

我忘了在我的spec文件中要求’spec_helper’.