【问题标题】:Ruby Minitest assert_equal failsRuby Minitest assert_equal 失败
【发布时间】:2017-10-05 16:31:40
【问题描述】:

我有一个包含以下内容的 gemfile:

# frozen_string_literal: true
source "https://rubygems.org"

gem 'cucumber'
gem 'minitest', '~> 5.10', '>= 5.10.3'
gem 'minitest-focus'
gem 'minitest-reporters', '~> 1.1.9'
gem 'rspec'
...

一个 Cucumber .env 文件,用于加载和需要 gem:

require 'bundler'

Bundler.require

还有一个包含以下内容的 Ruby 文件:

require 'minitest/autorun'
require_relative './../../../../RubyProjects/mksta-common/common'

class VerifyApi < MiniTest::Test

  include Common

  def initialize(has_authorization)
    @has_authorization = has_authorization
  end

  def test_id_correct
    assert_equal(20, 20)
  end
end

我在尝试执行该断言时收到此错误:

nil:NilClass 的未定义方法 `+'

在 Assertions.rb 中:

def assert_equal exp, act, msg = nil
      msg = message(msg, E) { diff exp, act }
      result = assert exp == act, msg

      if exp.nil? then
        if Minitest::VERSION =~ /^6/ then
          refute_nil exp, "Use assert_nil if expecting nil."
        else
          where = Minitest.filter_backtrace(caller).first
          where = where.split(/:in /, 2).first # clean up noise

          warn "DEPRECATED: Use assert_nil if expecting nil from #{where}. This will fail in Minitest 6."
        end
      end

      result
    end

def assert test, msg = nil
      self.assertions += 1
      unless test then
        msg ||= "Expected #{mu_pp test} to be truthy."
        msg = msg.call if Proc === msg
        raise Minitest::Assertion, msg
      end
      true
    end

错误发生在以下行:“self.assertions += 1”,所以我不确定“assertions”在哪里没有设置..

我想知道我的要求流程是否不正确,或者我是否缺少要求。或者也许 Cucumber / Rspec 正在阻碍?任何帮助将不胜感激。

【问题讨论】:

  • 您在某处为 nil 调用 +...它不在发布的代码中,所以它必须在您未发布的代码中
  • 检查完整的堆栈跟踪以了解此错误发生的位置,然后找出不应该出现的 nil 变量。好消息是您在测试中发现了这一点,而不是在部署应用程序之后。
  • @SimpleLime 我已经在 MiniTest 中使用发生错误的代码编辑了我的问题。

标签: ruby minitest


【解决方案1】:

好吧,问题是当你初始化VerifyApi时,你没有初始化MiniTest::Test。所以 self.assertions 是零。 要解决这个问题,你只需要在初始化函数中多加一行

super(name)

当然,你需要在函数中添加一个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多