Mock Clickatell API

68 views
Skip to first unread message

gerhardk

unread,
Dec 7, 2011, 8:18:37 AM12/7/11
to mocha-developer
Environment:
Rails: 3.0.10
Jruby: 1.6.5(1.9.2)
Clickatell gem: 0.8.2
Mocha: 0.10.0

Is there a way to mock the Clickatell::API.

What I Need:

Clickatell::API.authenticate('api_id', 'user', 'password') =>
<Clickatell::API:0x19a7f62 @auth_options={:session_id=>"session_id"}>
Thanks

James Mead

unread,
Dec 7, 2011, 8:44:19 AM12/7/11
to mocha-d...@googlegroups.com
It's not really clear what you are trying to do. I'm guessing you want to *stub* the call to API.authenticate, but it's not clear whether you want to return a real or a mock object. Either way, have you tried something like this :-

    api = Clickatell::API.new
    Clickatell::API.stubs(:authenticate).returns(api)

It would be better if you could give us an example test (or at least sketch it out in pseudo-code) and then we could give you more specific advice.

Something else to note is that it's better to wrap an external dependency like this in a domain-specific adapter to keep any stubbing of "code you don't own" to a minimum.

James.
----

gerhardk

unread,
Dec 7, 2011, 9:51:00 AM12/7/11
to mocha-developer
Hey James

Thanks for the reply.


What want to do is test a controller:
if Rails.env.production? || Rails.env.test?
begin
api = Clickatell::API.authenticate('api_id', 'user', 'password')
message_id = api.send_message(user.clickatel_phone_number, "Hello
World")
message_status = api.message_status(message_id)
### Update a object
user.update_attributes(:password => otp)
rescue SocketError => e
logger.debug(e.message)
flash[:notice] = "Connection to Clickatell Server is down'
end
end

I have 2 scenarios.
The First is the user is on the internet and can get to the
Clickatell::API, it generates a password, updates the user.password
and sends password via sms.
The Second is the user is not on the internet then should not try to
send sms and keep the password.

I want to mock the function authenticate and if it authenticate and
can send a message and then changes the user object (Mock what happens
when request is send to Clickatell::Api). Otherwise if its not on the
internet it should raise a SocketError (Mock the SocketError)

gerhardk

unread,
Dec 7, 2011, 10:31:31 AM12/7/11
to mocha-developer
My Test

it "should make a http request to clickatell" do
lambda{
stub_request(:get, "http://api.clickatell.com/http/auth?
api_id=api_id&password=password&user=user").to_return(:body
=>{:auth_options => {:session_id => "session_id"}})
}.should_not raise_error ### this does not work
### this also does not work
mock("Clickatell::API", "authenticate" => {:auth_options =>
{:session_id => "76485fa246812ad2be7da75346490a6a"}})
lambda {post :controller_action, :user_info => {:user_id =>
1}.should change(user, :password)
end

floehopper

unread,
Dec 26, 2011, 9:10:53 AM12/26/11
to mocha-developer
Sorry I've not had time to reply to you sooner. I had hope someone
else on the list might have been able to help you.

Unless I've misunderstood, the simplest way to use Mocha to raise the
exception you mention is :-

Clickatell::API.stubs(:authenticate).raises(SocketError)

You will need to set up this stub before you invoke the controller
action.

I'm a bit confused why you are using both Mocha and what appears to be
WebMock (i.e. the call to `stub_request`). If you want to completely
stub out the Clickatell API using just Mocha, you could do something
like this for the non-exception scenario :-

api = stub("Clickatell::API", :send_message =>
"message_id", :message_status => "message_status")
Clickatell::API.stubs(:authenticate).returns(api)

I hope that helps.

Regards, James.
----
http://jamesmead.org/

Gerhard Koekemoer

unread,
Jan 1, 2012, 3:50:26 PM1/1/12
to mocha-d...@googlegroups.com
Hey James

Thanks for the reply will try it when I'm back at the office.

Gerhard

Clickatell::API.stubs(:authenticate).raises(SocketError)

I hope that helps.

--
You received this message because you are subscribed to the Google Groups
"mocha-developer" group.
To post to this group, send email to mocha-d...@googlegroups.com.
To unsubscribe from this group, send email to
mocha-develop...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mocha-developer?hl=en.

Reply all
Reply to author
Forward
0 new messages