I am writing a test application using twitter, and I would like to write an integration test, but I cannot figure out how to mock objects in the Twitter namespace. Here is the function 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 is the rspec test 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 get 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
The above uses rspec, but I'm open to trying moka too. Any help would be greatly appreciated.
Well, I managed to figure it out with every 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 found out that I needed to stub :configure and :new on real objects and stub :current_user and :name on the dobuled object instance.
spinlock
source share