gpt4 book ai didi

ruby-on-rails-4 - 运行 rspec 时未初始化的常量 FactoryGirl,但在控制台中工作

转载 作者:行者123 更新时间:2023-12-04 07:21:02 24 4
gpt4 key购买 nike

这是我第一次使用 rspec。运行 rspec 时,我得到了 uninitialized constant 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:

# 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_railsFactoryGirl.find_definitions因为在指南中它只是说“你需要做的就是运行FactoryGirl.find_definitions "

最佳答案

我忘记在我的规范文件中 require 'spec_helper'

关于ruby-on-rails-4 - 运行 rspec 时未初始化的常量 FactoryGirl,但在控制台中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com