如何使用 twitter gem 和 capybara 进行集成测试?
我正在编写一个示例应用程序,使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的第一个注意事项是,您可以在没有 Twitter gem 的情况下获得他们的 Twitter 名称。我已将字段(twitter_handle、real_name)重命名为具体的。
然后您可以使用omniauth 测试模式对此进行测试。在你的 Cucumber 中钩子或 rspec 助手之前的某个地方
要测试,只需断言/应该你的用户名现在是“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.
You can then test this with omniauth test mode. Somewhere in your Cucumber before hooks or rspec helpers
To test, just assert/should that your user's name is now either "joshcrews" or "Josh Crews" depending on what you are looking for