使用黄瓜在rails(使用Koala)上测试facebook canvas应用程序

时间:2011-04-06 17:26:59

标签: ruby-on-rails tdd cucumber

如何伪造current_user方法以阻止我的黄瓜测试失败?

类似于Given /^I am logged in$/步骤。

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

1 个答案:

答案 0 :(得分:1)

我对Koala不熟悉,但这些是我成功与其他auth框架一起使用的一些想法。

  1. 定义current_user以在测试模式下表现不同。这有点难看。

    class ApplicationController < ActionController::Base
      ...
      def current_user
        if Rails.env == 'test'
          User.create! :username => 'Tester'
        else
          # do normal stuff
        end
      end
      ..
    end
    
  2. 创建一个为您登录用户的步骤,并在每个方案的开头使用它

    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
    
  3. 这假设您使用Capybara为Cucumber供电。 Webrat可能看起来不同;看看features / step_definitions / web_steps.rb以获得一些想法。

相关问题