0
我正在尝试学习MiniTest并通过这样做,我已经开始测试使用PayPal API批准/拒绝信用卡付款的应用程序之一。以下是我在测试Payment类中的购买方法的尝试。 (CREDIT_CARD原本是一个私有方法,转移到公众进行测试)MiniTest ::对于应该返回true的测试返回false的断言
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 :: Assertion:断言失败,没有给出的消息。 – Ctpelnar1988
感谢您纠正我的私人方法测试问题btw 。我一直在努力尝试它的语法。 – Ctpelnar1988
您是否更改设置进入def设置? –