使用 RSpec、should_receive 和 create 测试 setter?

发布于 2024-12-02 20:12:48 字数 1396 浏览 0 评论 0原文

我正在开发一个电子商务系统并熟悉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2024-12-09 20:12:48

我想说你的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 of create! 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 letting process_payment save to the database then call Payment.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文