All tests throwing TypeError: no implicit conversion of nil into String

All tests throwing TypeError: no implicit conversion of nil into String

更新 20180209:我创建了一个新的虚拟应用程序,其中包含初始 Rails 5.1.4 安装(包括 minitest 5.11.3)和测试,没有问题。将继续试验 gem 文件,看看我是否无法缩小世界范围内可能导致此问题的范围...

如果您有任何想法或指点,请告诉我!


我最近开始测试我的代码库的一个新部分,我发现了一个非常关键的问题。无论出于何种原因,每个测试现在都会抛出标题错误。查看跟踪,由于某种原因,我的测试中引用的 name/title 似乎导致了问题。不幸的是,我 知道 正在通过的 运行 测试(尽管承认在早期的 gem 版本中)会产生相同的结果,如下面的用户模型所示。

我已经尝试了所有我知道的方法,包括完全删除和重新安装 RVM、Rails 和 Ruby。我什至尝试过将 Minitest 版本控制到更早的版本,例如 10.5.3,但仍然没有成功。我花了一天的大部分时间试图解决这个问题;我束手无策,迫切需要帮助!虽然我已经学会了艰难的方法,但我现在需要明确地版本保护我所有的 gems ;)

提前致谢!如果您需要我添加更多内容,请告诉我 files/snippets。我会在我的日程安排允许的情况下尽力回复任何问题,希望能在 24 小时内回复。

返回的跟踪:

E

Error: UserTest#test_should_be_valid: TypeError: no implicit conversion of nil into String

/home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/railties-5.1.4/lib/rails/test_unit/reporter.rb:70:in method': undefined methodtest_should_be_valid' for class Minitest::Result' (NameError) from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/railties-5.1.4/lib/rails/test_unit/reporter.rb:70:informat_rerun_snippet' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/railties-5.1.4/lib/rails/test_unit/reporter.rb:23:in record' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:803:inblock in record' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:802:in each' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:802:inrecord' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:334:in run_one_method' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:321:inblock (2 levels) in run' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:320:in each' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:320:inblock in run' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:360:in on_signal' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:347:inwith_info_handler' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:319:in run' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/railties-5.1.4/lib/rails/test_unit/line_filtering.rb:9:inrun' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:159:in block in __run' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:159:inmap' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:159:in __run' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:136:inrun' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/minitest-5.11.3/lib/minitest.rb:63:in block in autorun' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/spring-2.0.2/lib/spring/application.rb:171:infork' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/spring-2.0.2/lib/spring/application.rb:171:in serve' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/spring-2.0.2/lib/spring/application.rb:141:inblock in run' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/spring-2.0.2/lib/spring/application.rb:135:in loop' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/spring-2.0.2/lib/spring/application.rb:135:inrun' from /home/blake/.rvm/gems/ruby-2.4.1@land_app/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in <top (required)>' from /home/blake/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:inrequire' from /home/blake/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in require' from -e:1:in'

这是一个相当标准的用户模型测试,之前没有出现过问题...

user.rb

class User < ApplicationRecord
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX =  /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
...
end

test/user_test.rb

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com",
    password: "password", password_confirmation: "password")
  end

  test "should be valid" do
    assert @user.valid?
  end
...
end

test_helper.rb

ENV['RAILS_ENV'] ||= 'test'

# NOTE: Disabling simplecov, trying to see if it is what is interfering with my backtrace_silencer settings
# require 'simplecov'
# SimpleCov.start
# require 'simplecov-json'
# SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter

require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

# Integrate AASM gem's custom testing methods into minitest
require 'aasm/minitest'

# NOTE: Minitest-reporters may overwrite backtrace_silencer settings. The below is supposed to force m-r to use the
# default Rails Minitest.backtrace_filter instead, though I'm still having trouble...
# https://github.com/kern/minitest-reporters
# require 'minitest/reporters'
# Minitest::Reporters.use!(
#   Minitest::Reporters::ProgressReporter.new,
#   ENV,
#   Minitest.backtrace_filter)
# require 'fileutils'

class CarrierWave::Mount::Mounter
  def store!
    # Not storing uploads in the tests
  end
end


class ActiveSupport::TestCase
  include ApplicationHelper
  include ActionDispatch::TestProcess

  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # https://github.com/carrierwaveuploader/carrierwave/wiki/Using-Carrierwave-with-Minitest-and-Rails-5
  # CarrierWave setup and teardown
  carrierwave_template = Rails.root.join('test', 'fixtures', 'files')
  carrierwave_root = Rails.root.join('test', 'support', 'carrierwave')
  carrierwave_cache_dir = Rails.root.join('test', 'support', 'carrierwave', 'carrierwave_cache')
  ##
  CarrierWave.configure do |config|
    config.root = carrierwave_root
    config.enable_processing = false
    config.storage = :file
    config.cache_dir = carrierwave_cache_dir
  end
  ##
  puts "*** Copying ***\n #{carrierwave_template.join('uploads').to_s}\n to\n #{carrierwave_root.to_s}"
  FileUtils.cp_r carrierwave_template.join('uploads'), carrierwave_root
  ##
  at_exit do
    puts "*** Removing carrierwave test directories ***"
    Dir.glob(carrierwave_root.join('*')).each do |dir|
      #puts " #{dir}"
      FileUtils.remove_entry(dir)
    end
  end

  # CarrierWave.root = Rails.root.join('test/fixtures/files')

  # FIXME: This seems to be causing some issues...
  # def after_teardown
  #   super
  #   # Default clean schedule is one day, this changes is to immediately after use
  #   CarrierWave.clean_cached_files!(0)
  # end

  # Returns true if a test user is logged in
  def is_logged_in?
    !session[:user_id].nil?
  end

  # log in as particular user
  def log_in_as(user)
    session[:user_id] = user.id
  end
end

class ActionDispatch::IntegrationTest

  # Log in as a particular user
  def log_in_as(user, password: 'password', remember_me: '1')
    post login_path, params: { session: { email: user.email,
                                          password: password,
                                          remember_me: remember_me } }
  end
end

我现在 运行

我的gem列表如下:

  * CFPropertyList (2.3.6)
  * aasm (4.12.3)
  * actioncable (5.1.4)
  * actionmailer (5.1.4)
  * actionpack (5.1.4)
  * actionview (5.1.4)
  * activejob (5.1.4)
  * activemodel (5.1.4)
  * activerecord (5.1.4)
  * activesupport (5.1.4)
  * area (0.10.0)
  * arel (8.0.0)
  * ast (2.4.0)
  * autoprefixer-rails (7.2.5)
  * bcrypt (3.1.11)
  * better_errors (2.4.0)
  * bindex (0.5.0)
  * binding_of_caller (0.8.0)
  * bootstrap-sass (3.3.7)
  * bootstrap-will_paginate (1.0.0)
  * builder (3.2.3)
  * bullet (5.7.2)
  * bundler (1.16.1)
  * carrierwave (1.2.2)
  * carrierwave-imageoptimizer (1.4.0)
  * coderay (1.1.2)
  * coffee-rails (4.2.2)
  * coffee-script (2.4.1)
  * coffee-script-source (1.12.2)
  * concurrent-ruby (1.0.5)
  * crass (1.0.3)
  * debug_inspector (0.0.3)
  * domain_name (0.5.20170404)
  * erubi (1.7.0)
  * excon (0.60.0)
  * execjs (2.7.0)
  * faker (1.8.7)
  * fastercsv (1.5.5)
  * ffi (1.9.21)
  * fission (0.5.0)
  * flamegraph (0.9.5)
  * fog (1.42.0)
  * fog-aliyun (0.2.0)
  * fog-atmos (0.1.0)
  * fog-aws (2.0.0)
  * fog-brightbox (0.14.0)
  * fog-cloudatcost (0.1.2)
  * fog-core (1.45.0)
  * fog-digitalocean (0.3.0)
  * fog-dnsimple (1.0.0)
  * fog-dynect (0.0.3)
  * fog-ecloud (0.3.0)
  * fog-google (0.1.0)
  * fog-internet-archive (0.0.1)
  * fog-joyent (0.0.1)
  * fog-json (1.0.2)
  * fog-local (0.4.0)
  * fog-openstack (0.1.23)
  * fog-ovirt (0.1.2)
  * fog-powerdns (0.1.1)
  * fog-profitbricks (4.1.1)
  * fog-rackspace (0.1.5)
  * fog-radosgw (0.0.5)
  * fog-riakcs (0.1.0)
  * fog-sakuracloud (1.7.5)
  * fog-serverlove (0.1.2)
  * fog-softlayer (1.1.4)
  * fog-storm_on_demand (0.1.1)
  * fog-terremark (0.1.0)
  * fog-vmfusion (0.1.0)
  * fog-voxel (0.1.0)
  * fog-vsphere (1.13.1)
  * fog-xenserver (0.3.0)
  * fog-xml (0.1.3)
  * formatador (0.2.5)
  * fuzzy_match (2.1.0)
  * geocoder (1.4.5)
  * globalid (0.4.1)
  * guard (2.14.2)
  * guard-compat (1.2.1)
  * guard-minitest (2.4.6)
  * http-cookie (1.0.3)
  * i18n (0.9.3)
  * image_optimizer (1.7.2)
  * inflecto (0.0.2)
  * ipaddress (0.8.3)
  * jbuilder (2.7.0)
  * jquery-rails (4.3.1)
  * json (2.1.0)
  * listen (3.1.5)
  * loofah (2.1.1)
  * lumberjack (1.0.12)
  * mail (2.7.0)
  * memory_profiler (0.9.8)
  * method_source (0.8.2)
  * mime-types (3.1)
  * mime-types-data (3.2016.0521)
  * mini_magick (4.8.0)
  * mini_mime (1.0.0)
  * mini_portile2 (2.3.0)
  * minitest (5.11.3)
  * multi_json (1.13.1)
  * mustermann (1.0.1)
  * nenv (0.3.0)
  * netrc (0.11.0)
  * nio4r (2.2.0)
  * nokogiri (1.8.2)
  * notiffany (0.1.1)
  * parser (2.4.0.2)
  * pg (0.21.0)
  * pry (0.10.4)
  * pry-nav (0.2.4)
  * pry-rails (0.3.6)
  * puma (3.11.2)
  * rack (2.0.4)
  * rack-mini-profiler (0.10.7)
  * rack-protection (2.0.0)
  * rack-test (0.8.2)
  * rails (5.1.4)
  * rails-controller-testing (1.0.2)
  * rails-dom-testing (2.0.3)
  * rails-html-sanitizer (1.0.3)
  * railties (5.1.4)
  * rake (12.3.0)
  * rb-fsevent (0.10.2)
  * rb-inotify (0.9.10)
  * rbovirt (0.1.5)
  * rbvmomi (1.11.6)
  * rest-client (2.0.2)
  * ruby_dep (1.5.0)
  * sass (3.5.5)
  * sass-listen (4.0.0)
  * sass-rails (5.0.7)
  * shellany (0.0.1)
  * simple_form (3.5.0)
  * sinatra (2.0.0)
  * slop (3.6.0)
  * solargraph (0.17.1)
  * spring (2.0.2)
  * spring-watcher-listen (2.0.1)
  * sprockets (3.7.1)
  * sprockets-rails (3.2.1)
  * stackprof (0.2.11)
  * thor (0.20.0)
  * thread_safe (0.3.6)
  * tilt (2.0.8)
  * trollop (2.1.2)
  * turbolinks (5.1.0)
  * turbolinks-source (5.1.0)
  * tzinfo (1.2.5)
  * uglifier (4.1.6)
  * unf (0.1.4)
  * unf_ext (0.0.7.5)
  * uniform_notifier (1.11.0)
  * web-console (3.5.1)
  * websocket-driver (0.6.5)
  * websocket-extensions (0.1.3)
  * will_paginate (3.1.6)
  * xml-simple (1.1.5)
  * yard (0.9.12)

当事情按预期工作时,通过恢复到较早的提交来解决这个问题,并通过 $ git checkout <branch> <file1> <file2> 方法引入所需的更新。

如果我不得不猜测,我认为我遇到的问题可能与我通过 RVM 对 2.5.0 的干预更新有关,这带来了比它值得的更多的麻烦(不是 Rails'这是错误,甚至是 RVM 的错误,因为 RVM 尚未正式支持 2.5.0。