Testing a facebook canvas app on rails (using Koala) with cucumber
How can I fake the current_user method to stop my cucumber tests failing?
Something like a Given /^I am logged in$/
step.
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
def facebook_cookies
@facebook_cookies ||= Koala::Facebook::OAuth.new.get_user_info_from_cookie(cookies)
end
def current_user
@graph = Koala::Facebook::GraphAPI.new(facebook_cookies['access_token'])
@current_user = User.find_by_fbid(开发者_如何转开发@graph.get_object("me")["id"]) || User.create(:fbid => @graph.get_object("me")["id"])
end
end
I'm not familiar with Koala, but these are some ideas I've successfully used with other auth frameworks.
Define current_user to behave differently in test mode. It's a little ugly.
class ApplicationController < ActionController::Base ... def current_user if Rails.env == 'test' User.create! :username => 'Tester' else # do normal stuff end end .. end
Create a step that logs in a user for you, and just use it at the beginning of each scenario
Given /^I am logged in$/ do passwd = 'realgoodpassword' user = User.create! :username => 'Tester', :password => passwd visit login_path fill_in 'Username', :with => user.username fill_in 'Password', :with => passwd click_button 'Sign in' end
This assumes you are using Capybara to power Cucumber. Webrat might look different; take a look in features/step_definitions/web_steps.rb to get some ideas.
精彩评论