Expectations: raising an exception that takes multiple arguments

61 views
Skip to first unread message

lesfreeman

unread,
Mar 17, 2010, 11:50:49 AM3/17/10
to mocha-developer
Hello,
I'm trying to do something like this:
@sms.expects(:create).raises(Clickatell::API::Error, "001",
"error_message")

The Clickatell::API::Error takes two arguments to its constructor, an
error code and an error message. Unfortunately, the above code causes
Mocha to choke:

ArgumentError: wrong number of arguments (3 for 2)
./test/functional/sms_message_controller_test.rb:40:in `raises'


So I also tried passing the arguments as an array:
@sms.expects(:create).raises(Clickatell::API::Error, ["001",
"error_message"])

But that causes the Error class to choke:
ArgumentError: wrong number of arguments (1 for 2)
/Users/lesfreeman/Sites/pvpowered/portal/branches/TRY-LF-test_coverage/
config/../vendor/plugins/mocha/lib/mocha/exception_raiser.rb:11:in
`initialize'

It appears that it doesn't try to unwrap the array before passing it
along to the error class.

I'm going to try monkeypatching the error class to take an array for
the initializer, but wanted to check and see if there was something I
was missing about how to get mocha to handle this out of the box.

Thanks!
Les

James Mead

unread,
Mar 17, 2010, 3:07:51 PM3/17/10
to mocha-d...@googlegroups.com

Hi Les,

You should be able to pass an instance of the exception into Expectation#raises.

I've knocked up a quick example here [1].

I've realised that this is not made clear in the docs - so I've added
a ticket [2] to improve the docs.

I hope that helps,

Cheers, James.

[1] http://pastie.org/874269
[2] http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/65

lesfreeman

unread,
Mar 18, 2010, 10:54:24 AM3/18/10
to mocha-developer
James,
Thanks for the quick reply. Worked like a charm!

On Mar 17, 12:07 pm, James Mead <jamesmea...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages