使用 RSpec、should_receive 和 create 测试 setter?
我正在开发一个电子商务系统并熟悉 RSpec。
订单有关联的付款。订单完成后,它开始通过我们的支付网关处理付款。
以下是规范、适当的方法和失败消息。
require 'spec_helper'
describe Order, "#process_payment" do
let!(:user) { create_user }
let!(:credit_card) { new_credit_card }
let!(:order) { user.orders.create! total: 200 }
let!(:payment) { stub('payment', started_processing!: true, ) }
before do
credit_card.stub(sale: true)
order.stub(credit_card: credit_card)
order.payments.stub(create!: payment)
payment.stub(:source= => true, process!: true)
end
it "creates a payment" do
order.payments.should_receive(:create!).
with(amount: 200, source: credit_card).and_return payment
order.process_payment
end
it "sets the payment's source to the credit card and tells it to process!" do
payment.should_receive(:source=).with(credit_card).ordered
payment.should_receive(:process!).ordered
order.process_payment
end
end
这是订单类。
class Order < ActiveRecord::Base
...
def process_payment
payments.create!(amount: total, source: credit_card).tap do |payment|
payment.process!
end
end
...
end
第二个规范失败,声称从未收到 :source= 。下单了,上面就只说了流程!被无序调用。如果没有订购,它会说它期望 :source= 一次,但从未收到它。我理解为什么 ordering 应该在那里,但只是想澄清它声称 :source= 从未被收到。
是创造!不调用源=?测试确保付款来源正确设置的适当方法是什么?
I'm working on an e-commerce system and familiarizing myself with RSpec.
An order has an associated payment. When the order is completed it starts to process the payment via our payment gateway.
Below is the spec, the appropriate method, and the failure message.
require 'spec_helper'
describe Order, "#process_payment" do
let!(:user) { create_user }
let!(:credit_card) { new_credit_card }
let!(:order) { user.orders.create! total: 200 }
let!(:payment) { stub('payment', started_processing!: true, ) }
before do
credit_card.stub(sale: true)
order.stub(credit_card: credit_card)
order.payments.stub(create!: payment)
payment.stub(:source= => true, process!: true)
end
it "creates a payment" do
order.payments.should_receive(:create!).
with(amount: 200, source: credit_card).and_return payment
order.process_payment
end
it "sets the payment's source to the credit card and tells it to process!" do
payment.should_receive(:source=).with(credit_card).ordered
payment.should_receive(:process!).ordered
order.process_payment
end
end
Here is the Order class.
class Order < ActiveRecord::Base
...
def process_payment
payments.create!(amount: total, source: credit_card).tap do |payment|
payment.process!
end
end
...
end
The second spec is failing, claiming that :source= is never received. With ordered, it just says that process! is called out of order. Without ordered, it says that it expected :source= once, but never received it. I understand why ordered should be there, but just wanted to be clear that it is claiming that :source= is never being received.
Is create! not calling source=? What is the appropriate way to test to make sure the source is being set on the payment correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说你的
it“create a payment”
规范就足够了。create!
的行为责任应该在专门为其编写的规范中。如果您确实想确保
create!
正在执行其应有的操作,也许可以尝试让process_ payment
保存到数据库,然后调用Payment.last
获取新记录并检查其属性。但我认为这太过分了。这样想,当您编写控制器规范时,您不会再次测试模型的所有行为;您只需测试控制器如何与它们交互。这是类似的情况。
I would say that your
it "creates a payment"
spec is enough. The responsibility for the behavior ofcreate!
should be in a spec written specifically for it.If you really want to make sure
create!
is doing what it's supposed to, perhaps try lettingprocess_payment
save to the database then callPayment.last
to get the new record and check the properties on it. But I think that would be overkill.Think of it this way, when you write controller specs you don't test all the behavior of your models again; you just test how your controller interfaces with them. This is a similar situation.