Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:42:40+00:00 2026-05-27T04:42:40+00:00

I’m writing a test app using the twitter gem and I’d like to write

  • 0

I’m writing a test app using the twitter gem and I’d like to write an integration test but I can’t figure out how to mock the objects in the Twitter namespace. Here’s the function that I want to test:

def build_twitter(omniauth)
  Twitter.configure do |config|
    config.consumer_key = TWITTER_KEY
    config.consumer_secret = TWITTER_SECRET
    config.oauth_token = omniauth['credentials']['token']
    config.oauth_token_secret = omniauth['credentials']['secret']
  end
  client = Twitter::Client.new
  user = client.current_user
  self.name = user.name
end

and here’s the rspec test that I’m trying to write:

feature 'testing oauth' do
  before(:each) do
    @twitter = double("Twitter")
    @twitter.stub!(:configure).and_return true
    @client = double("Twitter::Client")
    @client.stub!(:current_user).and_return(@user)
    @user = double("Twitter::User")
    @user.stub!(:name).and_return("Tester")
  end

  scenario 'twitter' do

    visit root_path
    login_with_oauth

    page.should have_content("Pages#home")
  end
end

But, I’m getting this error:

1) testing oauth twitter
   Failure/Error: login_with_oauth
   Twitter::Error::Unauthorized:
     GET https://api.twitter.com/1/account/verify_credentials.json: 401: Invalid / expired Token
   # ./app/models/user.rb:40:in `build_twitter'
   # ./app/models/user.rb:16:in `build_authentication'
   # ./app/controllers/authentications_controller.rb:47:in `create'
   # ./spec/support/integration_spec_helper.rb:3:in `login_with_oauth'
   # ./spec/integration/twit_test.rb:16:in `block (2 levels) in <top (required)>'

The mocks above are using rspec but I’m open to trying mocha too. Any help would be greatly appreciated.

OK, I managed to figure this out thanks to everyone’s help. Here’s the final test:

feature 'testing oauth' do
  before(:each) do
    @client = double("Twitter::Client")
    @user = double("Twitter::User")
    Twitter.stub!(:configure).and_return true
    Twitter::Client.stub!(:new).and_return(@client)
    @client.stub!(:current_user).and_return(@user)
    @user.stub!(:name).and_return("Tester")
  end

  scenario 'twitter' do

    visit root_path
    login_with_oauth

    page.should have_content("Pages#home")
  end
end

The trick was figuring out that I needed to stub :configure and :new on the real objects and stub :current_user and :name on a dobuled object instance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:42:41+00:00Added an answer on May 27, 2026 at 4:42 am

    I think the problem is just the way you are using the mock, you created the mock @twitter, but you never actually use it. I think you may be under the impression that any calls to Twitter will use the stubbed methods you specified, but that’s not how it works, only calls made to @twitter are stubbed.

    I use double ruby, not rspec mocks, but i believe you want to do something like this instead:

    Twitter.stub!(:configure).and_return true
    ...
    Twitter::Client.stub!(:current_user).and_return @user
    

    This ensures that anytime the methods you stubbed on Twitter, Twitter::Client are called, they respond how you want.

    Also, it seems strange that this is tested as part of a view, should really be part of a controller test instead unless i’m missing something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.