gpt4 book ai didi

ruby-on-rails - 了解如何使用 RSpec 测试类

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:52 24 4
gpt4 key购买 nike

我希望通过这个问题实现的主要目标是理解。在一些帮助下,我一直在考虑将我的 Controller 代码重构为更易于管理的模块/类,以便我可以有效地测试它们。我在这里有一个我想处理的例子,我的问题是我将如何测试 Sale 类:

class TransactionsController < ApplicationController
def create
payment = BraintreeTransaction::VerifyPayment.new(params, @user_id, @transaction_total)
payment.run(params)

if payment.success?
redirect_to thank_you_path
else
flash.now[:alert] = payment.error
flash.keep
redirect_to new_transaction_path
end
end

module BraintreeTransaction
class VerifyPayment
def initialize(params, user_id, total)
@transaction_total = total
@user_id = user_id
@params = params
@error_message = nil
end

def run(params)
@result = BraintreeTransaction::Sale.new.braintree_hash(params, @transaction_total)
if @result.success?
@cart_items = CartItem.where(user_id: @user_id).where.not(image_id: nil)
@cart_items.destroy_all
create_real_user
update_completed_transaction
guest_user.destroy
@success = true
else
update_transaction
@error_message = BraintreeErrors::Errors.new.error_message(@result)
end
end

def success?
@success
end

def error
@error_message
end
end

module BraintreeTransaction
class Sale
def braintree_hash(params, total)
Braintree::Transaction.sale(
amount: total,
payment_method_nonce: params[:payment_method_nonce],
device_data: params[:device_data],
customer: {
first_name: params[:first_name],
last_name: params[:last_name],
email: params[:email],
phone: params[:phone]
},
billing: {
first_name: params[:first_name],
last_name: params[:last_name],
company: params[:company],
street_address: params[:street_address],
locality: params[:locality],
region: params[:region],
postal_code: params[:postal_code]
},
shipping: {
first_name: params[:shipping_first_name].presence || params[:first_name].presence,
last_name: params[:shipping_last_name].presence || params[:last_name].presence,
company: params[:shipping_company].presence || params[:company].presence,
street_address: params[:shipping_street_address].presence || params[:street_address].presence,
locality: params[:shipping_locality].presence || params[:locality].presence,
region: params[:shipping_region].presence || params[:region].presence,
postal_code: params[:shipping_postal_code].presence || params[:postal_code].presence
},
options: {
submit_for_settlement: true,
store_in_vault_on_success: true
}
)
end
end
end

我不知道我是否看错了,但是这段代码 BraintreeTransaction::Sale.new.braintree_hash 是我想要测试的,我想确保在调用时该类收到一个哈希值?

更新

到目前为止我已经想到了这个(虽然我不是 100% 确信这是正确的方法?)

require 'rails_helper'
RSpec.describe BraintreeTransaction::Sale do
@transaction_total = 100
let(:params) { FactoryGirl.attributes_for(:braintree_transaction, amount: @transaction_total) }
it 'recieves a hash when creating a payment' do
expect_any_instance_of(BraintreeTransaction::Sale).to receive(:braintree_hash).with(params, @transaction_total).and_return(true)
end
end

我收到一个我不明白的错误返回

Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure }
Exactly one instance should have received the following message(s) but didn't: braintree_hash

最佳答案

我可能不准确,但我会按照我解决问题的方式来回答。您可以通过三种方式编写命中要测试的代码的测试。

  • BraintreeTransaction::Sale 对象编写 braintree_hash 单元测试
  • TransactionsController Controller 中的 create 方法编写一个 Controller 单元方法
  • TransactionsController 中的 create 方法编写路由集成测试。

这些是您可以开始探索的方式。

关于ruby-on-rails - 了解如何使用 RSpec 测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35451342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com