Ruby on rails MiniTest::对于应该返回true的测试,断言返回false

Ruby on rails MiniTest::对于应该返回true的测试,断言返回false,ruby-on-rails,ruby,minitest,Ruby On Rails,Ruby,Minitest,我正在尝试学习MiniTest,通过学习MiniTest,我已经开始测试我的一个应用程序,该应用程序使用PayPal API批准/拒绝信用卡支付。下面是我在Payment类中测试purchase方法的尝试。(信用卡最初是一种私人方法,现在已被公开测试) payment.rb require "active_merchant/billing/rails" class Payment < ActiveRecord::Base belongs_to :order attr_access

我正在尝试学习MiniTest,通过学习MiniTest,我已经开始测试我的一个应用程序,该应用程序使用PayPal API批准/拒绝信用卡支付。下面是我在Payment类中测试purchase方法的尝试。(信用卡最初是一种私人方法,现在已被公开测试)

payment.rb

require "active_merchant/billing/rails"

class Payment < ActiveRecord::Base
  belongs_to :order
  attr_accessor :card_number, :card_verification

  def purchase(card_info, billing_info)
    if credit_card(card_info).valid?
      response = GATEWAY.purchase(price_in_cents, credit_card(card_info), purchase_options(billing_info))
      @paypal_error = response.message
      response.success?
    end
  end

  def price_in_cents
    (@total.to_f * 100).round
  end


    def credit_card(card_info)
      @credit_card ||= ActiveMerchant::Billing::CreditCard.new(card_info)
    end

  private

  def purchase_options(billing_info)
    billing_info
  end

end
require 'test_helper'
require "active_merchant/billing/rails"

class PaymentTest < ActiveSupport::TestCase
  setup do
    @card_info = {
      brand: "Visa",
      number: "4012888888881881",
      verification_value: "123",
      month: "01",
      year: "2019",
      first_name: "Christopher",
      last_name: "Pelnar",
    }
    @purchase = Payment.new
  end

  test "purchase" do
    assert @purchase.credit_card(@card_info).valid?, true
  end

end
assert
方法调用使用语法
assert(test,msg=nil)
测试返回true的原因是您选择使用的消息。
assert_equal
方法需要比较两个值。另外,您可以使用如下方法来代替将私有方法公开,而不是使用
.send
方法:

assert @purchase.send(:credit_card,@card_info).valid?
另外,将设置更改为函数定义:

def setup
  # setup logic
end
要使输出更加详细(捕获ActiveMerchant错误),请尝试以下操作:

test "purchase" do
  credit_card = @purchase.send(:credit_card, @card_info)
  assert credit_card.valid?, "valid credit card"
  puts credit_card.errors unless credit_card.errors.empty?
end
Reading我认为在测试中信用卡类型应该设置为伪造。

assert
方法调用使用语法
assert(test,msg=nil)
测试返回true的原因是您选择使用的消息。
assert_equal
方法需要比较两个值。另外,您可以使用如下方法来代替将私有方法公开,而不是使用
.send
方法:

assert @purchase.send(:credit_card,@card_info).valid?
另外,将设置更改为函数定义:

def setup
  # setup logic
end
要使输出更加详细(捕获ActiveMerchant错误),请尝试以下操作:

test "purchase" do
  credit_card = @purchase.send(:credit_card, @card_info)
  assert credit_card.valid?, "valid credit card"
  puts credit_card.errors unless credit_card.errors.empty?
end

Reading我认为在测试中应该将信用卡类型设置为伪造。

我将方法改回private并使用您提供的逻辑运行测试,它返回的是:
Minitest::Assertion:Failed Assertion,没有给出任何消息。
谢谢您纠正我的私有方法测试问题。顺便说一句,我以前尝试过这种方法,但在语法上遇到了困难。您是否将安装程序do更改为def安装程序?我更改了。已更改为方法定义。我非常确定由于测试环境的原因,该卡未通过验证。我对答案进行了详细的编辑。我将方法改回private,并使用您提供的逻辑运行测试,它返回的是:
Minitest::Assertion:Failed Assertion,没有给出任何消息。
谢谢您纠正我的私有方法测试问题。顺便说一句,我以前尝试过这种方法,但在语法上遇到了困难。您是否将安装程序do更改为def安装程序?我更改了。已更改为方法定义。我非常确定由于测试环境的原因,该卡未通过验证。我对答案做了一些冗长的修改。