Ruby on rails 如何在Rspec中测试此类类行为
我有一个类负责处理来自支付网关的一些响应 比如说:Ruby on rails 如何在Rspec中测试此类类行为,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我有一个类负责处理来自支付网关的一些响应 比如说: class PaymentReceiver def initialize(gateway_response) @gateway_response = gateway_response end def handle_response if @gateway_response['NC_STATUS'] != '0' if order order.fail_payment else
class PaymentReceiver
def initialize(gateway_response)
@gateway_response = gateway_response
end
def handle_response
if @gateway_response['NC_STATUS'] != '0'
if order
order.fail_payment
else
raise 'LackOfProperOrder'
# Log lack of proper order
end
end
end
private
def order
@order ||= Order.where(id: @gateway_response['orderID']).unpaid.first
end
end
在支付的有效负载中,我的状态为NC\u
负责支付成功时的信息和orderID
,它引用orderactiverecord类by
id`
我想测试行为(在rspec中):
如果PaymentReceiver收到响应,其中NC_状态!=0将付款失败
发送到orderID
引用的特定Order
对象
您将如何进行测试?我认为设计也可能是糟糕的…您必须进行重构以删除
SRP
和DIR
原则冲突。
下面我要说:
class PaymentReceiver
def initialize(response)
@response = response
end
def handle_response
if @response.success?
@response.order.pay
else
@response.order.fail_payment
end
end
end
# it wraps output paramteres only !
class PaymentResponse
def initialize(response)
@response = response
end
def order
# maybe we can check if order exists
@order ||= Order.find(@response['orderID'].to_i)
end
def success?
@response['NCSTATUS'] == '0'
end
end
p = PaymentReceiver.new(PaymentResponse({'NCSTATUS' => '0' }))
p.handle_response
那么测试一切都很容易