Rspec模拟错误:参数数量错误

时间:2013-06-25 16:58:26

标签: ruby rspec stripe-payments

我试图使用Rspec来删除Stripe API,而我遇到了问题。这是我的代码的样子:

Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)

这是我得到的错误:

Failure/Error: Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)
ArgumentError:
  wrong number of arguments (0 for 3..6)

1 个答案:

答案 0 :(得分:19)

Stripe :: CardError需要3..6个参数,符合以下源代码:

class CardError < StripeError
  ...
  def initialize(message, param, code, http_status=nil, http_body=nil, json_body=nil)

以下是github上RSpec doc的关键文档:

expect(double).to receive(:msg).and_raise(error)
  #error can be an instantiated object or a class
  #if it is a class, it must be instantiable with no args

由于您只是提供类,而类需要参数,因此它失败了。您需要实例化它(即通过new)并提供参数。

完整定义位于https://github.com/stripe/stripe-ruby/blob/0c281891948a793e79cc997d31918ba7f987f7ae/lib/stripe/errors/card_error.rb