Forum: RSpec How to stub a class inside a module with Mocha in Ruby?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0ac8e1d6455de5e97f16fe961f1430f7?d=identicon&s=25 Toy C. (noppanit)
on 2013-01-29 14:15
I know there are lot of questions in here around this. I've tried all of
them but none of them works. Basically, I'm trying to stub
TwitterOAuth::Client so my test won't call the real api every time.

Here's the bit from TwitterOAuth gem that I use.

module TwitterOAuth
  class Client

    def initialize(options = {})
      @consumer_key = options[:consumer_key]
      @consumer_secret = options[:consumer_secret]
      @token = options[:token]
      @secret = options[:secret]
      @proxy = options[:proxy]
      @debug = options[:debug]
      @api_version = options[:api_version] || '1'
      @api_host = options[:api_host] || ''
      @search_host = options[:search_host] || ''

    def request_token(options={})
      consumer(:secure => true).get_request_token(options)

The request_token is the method I'm trying to stub. And this is what
I've done in Sinatra which obviously doesn't work.

before '/' do
   @twitterClient =

get '/login' do
   token = @twitterClient.request_token(callback)
   # do some other checking

And here's my test

it "should login with Twitter and save session" do

    #it's still going to the real twitter api.


I've read from somewhere that ruby mixin can be overridden in anywhere.
Clearly, I'm misunderstanding something.

The alternative way is I could wrap the gem again with class and then
stub from the class. Is that a good idea to do?

Please help.
This topic is locked and can not be replied to.