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
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.