【问题标题】:how can I do integration testing with the twitter gem and capybara?如何使用 twitter gem 和 capybara 进行集成测试?
【发布时间】:2011-11-28 21:00:21
【问题描述】:

我正在编写一个示例应用程序,使用 Devise + OmniAuth 进行登录,并使用 twitter gem 来获取用户名。我想添加一些集成测试,但我不知道如何处理 twitter gem。

这是我的用户模型(大部分逻辑都在这里):

def build_authentication(omniauth)
  # If the provider is twitter, get additional information                    
  # to build a user profile.                                                  
  if omniauth['provider'] == 'twitter'         
    self.build_twitter(omniauth)
  end

  # now put the authentication in the database                                
  authentications.build(:provider => omniauth['provider'],
                        :uid => omniauth['uid'],
                        :token => omniauth['credentials']['token'],
                        :secret => omniauth['credentials']['secret'])
end
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
  self.name = client.current_user.name
end

我已将以下内容添加到我的 spec_helper.rb 中,以便通过集成测试的登录部分:

OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = {
  'provider' => 'twitter',
  'uid' => '12345',
  'credentials' => {
    'token' => '12345',
    'secret' => '54321'
  }
}

但我不知道如何测试使用 twitter gem 的 build_twitter 方法。任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: ruby twitter integration-testing capybara omniauth


    【解决方案1】:

    我的第一个注意事项是,您可以在没有 twitter gem 的情况下获得他们的 twitter 名称。我已将字段(twitter_handle、real_name)重命名为具体的。

    self.twitter_handle ||= omniauth['user_info']['nickname']
    self.real_name = omniauth['user_info']['name']
    

    然后您可以使用omniauth 测试模式对此进行测试。在你的 Cucumber 中的某个地方,在钩子或 rspec 助手之前

      OmniAuth.config.test_mode = true
    
      # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
      OmniAuth.config.mock_auth[:twitter] = {
        "provider"=>"twitter", 
        "uid"=>"1694349", 
        "credentials"=>{
          "token"=>"165349-aRlUJ7TeIb4Ak57oqycgwihqobrzQ0k5EI7", 
          "secret"=>"SNZT7S70xZIhANfZzgHUEpZMPSsGEHw"
        }, 
        "user_info"=>{"nickname"=>"joshcrews", "name"=>"Josh Crews", "location"=>"Nashville, TN", "image"=>"http://a3.twimg.com/profile_images/1076036384/josh_profile_franklin_normal.jpg", "description"=>"Christian, Nashville web developer, Ruby", "urls"=>{"Website"=>"http://www.joshcrews.com", "Twitter"=>"http://twitter.com/joshcrews"}}
      }
    

    要进行测试,只需断言/应该您的用户名现在是“joshcrews”还是“Josh Crews”,具体取决于您要查找的内容

    【讨论】:

    • 感谢您的回答。我确实知道使用 twitter gem 效率不高,而且我可以从omniauth 哈希中获取名称,但这是一个我实际上想要同时设置omniauth 和twitter gem 的示例。
    • 酷。好吧,我对 Twitter gem 进行了测试,但非常非常有限。我使用 mocha 并使用“Twitter::Client.any_instance.expects(:update)”来确保在集成测试期间调用了该方法。
    • 值得添加另一个 gem/syntax 吗? Mocha 看起来很有趣,但我想重新编写代码而不是学习新的测试框架:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多