Rspec-rails: Rspec and_raise cant accept module class exception

Created on 12 May 2015  路  2Comments  路  Source: rspec/rspec-rails

Hi

I am using the Koala gem for facebook integration.
So basically, I wanna test the Koala::Facebook::APIError exception.
These are my codes:
On the User model code

  .... lots of code
rescue Koala::Facebook::APIError => e
   do something
end
   @graph = double(Koala::Facebook::API)
    allow(@graph).to receive(:get_object).and_raise(Koala::Facebook::APIError)
    allow(Koala::Facebook::API).to receive(:new).and_return(@graph)

Whenever I run the rspec, I kept getting the error of:

Failure/Error: allow(@graph).to receive(:get_object).and_raise(Koala::Facebook::APIError)
     ArgumentError:
       wrong number of arguments (0 for 2..3)

Any help to specify the and_raise for this issue?

Thanks

Most helpful comment

and_raise(x) winds up calling raise x which works as long as x can be instantiated without arguments. Unfortunately, Koala::Facebook::APIError requires 2 or 3 arguments:

https://github.com/arsduo/koala/blob/v2.0.0/lib/koala/errors.rb#L31

...which means that when RSpec calls raise Koala::Facebook::APIError it gets the error you see. There's nothing RSpec can do about that; there's no way for RSpec to guess what reasonable values for the required arguments could be. You need to instantiate the exception class yourself in this kind of case:

allow(@graph).to receive(:get_object).and_raise(Koala::Facebook::APIError.new(404, "Not Found"))

All 2 comments

and_raise(x) winds up calling raise x which works as long as x can be instantiated without arguments. Unfortunately, Koala::Facebook::APIError requires 2 or 3 arguments:

https://github.com/arsduo/koala/blob/v2.0.0/lib/koala/errors.rb#L31

...which means that when RSpec calls raise Koala::Facebook::APIError it gets the error you see. There's nothing RSpec can do about that; there's no way for RSpec to guess what reasonable values for the required arguments could be. You need to instantiate the exception class yourself in this kind of case:

allow(@graph).to receive(:get_object).and_raise(Koala::Facebook::APIError.new(404, "Not Found"))

Ah I see,
So the Koala that is needed the arguments, not the and_raise

My bad,

Thank you very much

Was this page helpful?
0 / 5 - 0 ratings