How to mock an instance method of an already mocked object?
Asked Answered
V

2

4

I need to mock the following:

Class User
  def facebook
    #returns an instance of a facebook gem
  end
end

So in my User tests, to access the User's facebook info I need to call user.facebook.me.info to retrieve its info. If I want to mock this, I'm currently using the following:

@user = Factory(:user)
facebook = mock()
me = mock()
me.expects(:info).returns({"name" => "John Doe"})
facebook.expects(:me).returns(me)
@user.expects(:facebook).returns(facebook)
assert_equal "John Doe", @user.facebook.me.info["name"]

This works but seems a bit unwieldy, is there a better way to do this ?

[edit] I'm using mocha as mocking framework

Verbify answered 24/10, 2010 at 14:33 Comment(0)
T
13

You could try something like this :-

user = Factory(:user)
user.stubs(:facebook => stub(:me => stub(:info => {:name => "John Doe"})))

If you really want to check that all these methods are called (which I suspect you don't), you could do the following :-

user = Factory(:user)
user.expects(:facebook => mock(:me => mock(:info => {:name => "John Doe"})))

It's a bit more verbose, but it's usually worthwhile giving each mock object a name :-

user = Factory(:user)
user.stubs(:facebook => stub('facebook', :me => stub('me', :info => {:name => "John Doe"})))

I hope that helps.

Tine answered 2/12, 2010 at 13:25 Comment(1)
That looks like the cleanest solution out there. Thanks!Verbify
A
0

If you don't want to check that all the methods are called, you can also use different alternatives to mocking. For instance, you can use an OpenStruct.

@user = Factory(:user)
facebook = mock()
me = mock()
me.expects(:info).returns({"name" => "John Doe"})
facebook.expects(:me).returns(me)
@user.expects(:facebook).returns(facebook)
assert_equal "John Doe", @user.facebook.me.info["name"]

becomes

@user = Factory(:user)
@facebook = OpenStruct.new(:me => OpenStruct.new(:info => {:name => "John Doe"}))
@user.expects(:facebook).returns(@facebook)

This solution also offers you the advantage that you can change the @facebook properties in your tests, if you need to test different conditions.

Astigmatism answered 2/12, 2010 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.