MiniTest::Assertions 为应该返回 true 的测试返回 false
MiniTest::Assertions returning false for test that should be returning true
我正在尝试学习 MiniTest,通过这样做,我已经开始测试我的一个使用 PayPal API 到 approve/deny 信用卡付款的应用程序。下面是我在 Payment class 中测试购买方法的尝试。 (credit_card本来是私有方法,为了测试搬到了public)
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
payment_test.rb
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
运行rake test
后的错误信息:
--------------------------
PaymentTest: test_purchase
--------------------------
(0.1ms) ROLLBACK
test_purchase FAIL (0.02s)
Minitest::Assertion: true
test/models/payment_test.rb:20:in `block in <class:PaymentTest>'
Finished in 0.03275s
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
MiniTest::Assertions assert
方法调用使用语法 assert(test, msg = nil)
您的测试返回 true 的原因是您选择使用的消息。 assert_equal
方法采用 2 个值进行比较。此外,您可以像这样使用 .send
方法,而不是创建私有方法 public:
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
正在阅读rubyforge API我觉得测试时应该把信用卡类型设置成假的
我正在尝试学习 MiniTest,通过这样做,我已经开始测试我的一个使用 PayPal API 到 approve/deny 信用卡付款的应用程序。下面是我在 Payment class 中测试购买方法的尝试。 (credit_card本来是私有方法,为了测试搬到了public)
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
payment_test.rb
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
运行rake test
后的错误信息:
--------------------------
PaymentTest: test_purchase
--------------------------
(0.1ms) ROLLBACK
test_purchase FAIL (0.02s)
Minitest::Assertion: true
test/models/payment_test.rb:20:in `block in <class:PaymentTest>'
Finished in 0.03275s
1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
MiniTest::Assertions assert
方法调用使用语法 assert(test, msg = nil)
您的测试返回 true 的原因是您选择使用的消息。 assert_equal
方法采用 2 个值进行比较。此外,您可以像这样使用 .send
方法,而不是创建私有方法 public:
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
正在阅读rubyforge API我觉得测试时应该把信用卡类型设置成假的