如何使用 twitter gem 和 capybara 进行集成测试?

发布于 2024-12-18 06:08:55 字数 1502 浏览 3 评论 0原文

我正在编写一个示例应用程序,使用 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 方法。任何帮助将不胜感激。

谢谢!

I'm writing an example app using Devise + OmniAuth for logging in and the twitter gem to get the users name. I'd like to add a few integration tests but I don't know what to do with the twitter gem.

Here's my user model (which is where most of the logic is found):

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

I've added the following to my spec_helper.rb so that I pass the login part of the integration test:

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

But I can't figure out how to test the build_twitter method which uses the twitter gem. Any help would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2024-12-25 06:08:55

我的第一个注意事项是,您可以在没有 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”,具体取决于你要查找的内容

My first note is that you can get their twitter name without the twitter gem. I've renamed the fields (twitter_handle, real_name) to be specific.

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

You can then test this with omniauth test mode. Somewhere in your Cucumber before hooks or rspec helpers

  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"}}
  }

To test, just assert/should that your user's name is now either "joshcrews" or "Josh Crews" depending on what you are looking for

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文