Stubbing rspec and_raise and adding post

I am writing tests that should check for rescues in my code.

Model code:

rescue Coinbase::Error => e
  #debugger
  if e == "You don't have that many bitcoins in your account to sell."
  ...
end

      

Rspec code:

allow_any_instance_of(Order).to receive(:sell).and_raise(Coinbase::Error, "You don't have that many bitcoins in your account to sell.")

      

By adding a debugger where I did it and looking at the value e

in the console I see

#<Coinbase::UnauthorizedError: Coinbase::UnauthorizedError>

Thus, the message is not transmitted.

I've been searching for the last 40 minutes and everything I've found only covers submitting the error class, not the message. Presumably, there are situations when there is the same class of errors, but different messages.

Any suggestions would be great. Thank!

+3


source to share


1 answer


I think what you want to do: Coinbase::Error.new("You don't have that many bitcoins in your account to sell.")

inside the raise.



Update, I think you also want to e.message == ""

not e == ""

, because you are comparing the error to a string, not an error message.

+2


source







All Articles