我们正在将Rails应用程序从3.2升级到5.0。我们到了4.2,测试套件通过了100%,现在是5.0.1,测试套件抛出了参数错误。
运行rake spec:models时,发生以下错误:
An error occurred while loading rails_helper.
Failure/Error: require File.expand_path("../config/environment", __dir__)
ArgumentError:
wrong number of arguments (given 0, expected 2..3)
Ruby V 2.5.1 RSpec V 3.8
在开发环境中运行rake db:migrate任务已完成。.
在测试环境中运行db:migrate之后,发生以下错误:
ArgumentError: wrong number of arguments (given 0, expected 2..3)
/home/vagrant/rtest/config/initializers/load_rtest_config.rb:5:in `test'
/home/vagrant/rtest/config/initializers/load_rtest_config.rb:5:in `<top
(required)>'
/home/vagrant/rtest/config/environment.rb:8:in `<top (required)>'
/home/vagrant/rtest/bin/rails:4:in `require'
/home/vagrant/rtest/bin/rails:4:in `<main>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
envrionment.rb:
# Load the rails application
# require File.expand_path('../application', __FILE__) # RAILS 4.2
require_relative 'application'
# Initialize the rails application
# Changes for rtest upgrade
Rails.application.initialize!
load_rtest_config.rb:
require 'ostruct'
require 'yaml'
config = OpenStruct.new(YAML.load_file("#{Rails.root}/config/rtest.yml"))
::RTestConfig = OpenStruct.new(config.send(Rails.env))
::SummaryConfig = OpenStruct.new(YAML.load_file("#
{Rails.root}/config/summary.yml"))
::LDAPConfig = OpenStruct.new(YAML.load_file("#
{Rails.root}/config/ldap.yml"))
相关宝石:
gem 'rails-controller-testing'
group :test do
gem 'factory_bot_rails', :require => false
gem 'capybara'
gem 'guard-rspec'
gem 'guard-spork'
gem 'shoulda'
gem 'rspec-its'
gem 'rubocop-rspec'
gem 'rspec-rails'
gem 'rails-controller-testing'
#Rails 5 Upgrade
# gem 'shoulda-matchers'
end
rails_helper.rb
require 'rubygems'
# require 'spork'
require 'database_cleaner'
require 'rspec/its'
# ActiveRecord::Migration.check_pending!
# ActiveRecord::Migration.maintain_test_schema!
require 'spec_helper'
require File.expand_path("../config/environment", __dir__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_bot_rails'
require 'capybara/rspec'
require 'authlogic/test_case'
ENV["RAILS_ENV"] ||= 'test'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.expose_current_running_example_as :example
config.use_transactional_fixtures = true
config.include FactoryBot::Syntax::Methods
config.include Shoulda::Matchers::ActiveModel, type: :model
config.include Shoulda::Matchers::ActiveRecord, type: :model
config.include Devise::Test::ControllerHelpers, :type => :controller
config.include LoginMacros, :type => :controller
config.include LoginMacrosRequest, :type => :request
config.after(:each) { Warden.test_reset! }
end
puts "Reloading controllers and models"
Dir["#{Rails.root}/app/controllers//*.rb"].each do |controller|
load controller
end
Dir["#{Rails.root}/app/models//*.rb"].each do |model|
load model
end
puts "Done"
FactoryBot.reload
Dir[Rails.root.join("spec/support/**/*.rb")].each do |f|
require f
end
编辑:抱歉,如果问题不清楚,这是我的第一个问题,谢谢。
答案 0 :(得分:0)
已解决!
问题出在这里
ArgumentError: wrong number of arguments (given 0, expected 2..3)
/home/vagrant/rtest/config/initializers/load_rtest_config.rb:5:in `test'
“测试”一词使红宝石认为这是一种方法测试(不知道在何处定义),需要2..3自变量。
load_rtest_config :
config = OpenStruct.new(YAML.load_file("#{Rails.root}/config/rtest.yml"))
::RTestConfig = OpenStruct.new(config.send(Rails.env))
上面的.send方法未正确传递字符串“ test”。但是它将通过“开发”。
FIX:
:: RTestConfig = OpenStruct.new(config [Rails.env])