¿Cómo puedo hacer pruebas de integración con la gema de Twitter y el capibara?
-
25-10-2019 - |
Pregunta
Estoy escribiendo una aplicación de ejemplo usando Disvise + Omniauth para iniciar sesión y la gema de Twitter para obtener el nombre del usuario. Me gustaría agregar algunas pruebas de integración, pero no sé qué hacer con la gema de Twitter.
Aquí está mi modelo de usuario (que es donde se encuentra la mayor parte de la lógica):
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
He agregado lo siguiente a mi spec_helper.rb para pasar la parte de inicio de sesión de la prueba de integración:
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '12345',
'credentials' => {
'token' => '12345',
'secret' => '54321'
}
}
Pero no puedo entender cómo probar el build_twitter
Método que usa la gema de Twitter. Cualquier ayuda sería muy apreciada.
¡Gracias!
Solución
Mi primera nota es que puedes obtener su nombre de Twitter sin la gema de Twitter. He cambiado el nombre de los campos (twitter_handle, real_name) para que sea específico.
self.twitter_handle ||= omniauth['user_info']['nickname']
self.real_name = omniauth['user_info']['name']
Luego puede probar esto con el modo de prueba Omniauth. En algún lugar de su pepino antes de los ganchos o ayudantes de 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"}}
}
Para probar, solo afirmar/si el nombre de su usuario ahora es "Joshcrews" o "Josh Crews" dependiendo de lo que esté buscando