简体   繁体   中英

Rails test Facebook methods using Koala and RSpec

I'm making an application that uses Facebook info with Rails, Omniauth and Koala. Using Ryan Bates Facebook Railscasts , I use this facebook method on my User model, to get the users uid, auth_token, etc from Facebook's API.

def facebook
  @facebook ||= Koala::Facebook::API.new(oauth_token)
end

I also have another method that calls the facebook method to get info from the user's friends, lets call this, facebook_friends

On my UsersController show action I have this:

def show
  @user = User.find(params[:id])
  @friends = @user.facebook_friends
end

So I use those two instance variables in my show template.

Writing tests with RSpec

Here's one test I'm trying to make

feature 'As a logged out user' do
  background do
    @user = FactoryGirl.create(:user)
  end
  scenario 'redirects to root path' do
    visit user_path @user
    page.current_path.should == root_path
  end
end

But when I run it, I get this error:

1) As a logged out user redirects to root path
 Failure/Error: visit user_path @user
 Koala::Facebook::APIError:
   OAuthException: Invalid OAuth access token.
 # ./app/models/user.rb:45:in `friends_birthday'
 # ./app/controllers/users_controller.rb:7:in `show'
 # ./spec/requests/user_integration_spec.rb:22:in `block (2 levels) in <top (required)>'

Since I dont want to use a valid access token everytime I run the tests How can I fake the facebook call on the User model using RSpec?

VCR是答案,您可以检查出色的Railscasts http://railscasts.com/episodes/291-testing-with-vcr在示例项目中可以找到一些示例: https : //github.com/lucassus/locomotive /blob/master/spec/features/user_facebook_connect_spec.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM