Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec mocks error: wrong number of arguments

I'm trying to stub out the Stripe API using Rspec and I'm running into an issue. Here's what my code looks like:

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

Here's the error I'm getting:

Failure/Error: Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)
ArgumentError:
  wrong number of arguments (0 for 3..6)
like image 719
LandonSchropp Avatar asked Jun 25 '13 16:06

LandonSchropp


1 Answers

Stripe::CardError requires 3..6 arguments, per the following source code:

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

Here's the key documentation from the RSpec doc at github:

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

Since you're just providing the class and the class requires arguments, it's failing. You need to instantiate it (i.e. via new) and provide arguments.

Full definition is in https://github.com/stripe/stripe-ruby/blob/0c281891948a793e79cc997d31918ba7f987f7ae/lib/stripe/errors/card_error.rb

like image 195
Peter Alfvin Avatar answered Oct 24 '22 23:10

Peter Alfvin