Pregunta

Tengo la siguiente clase simple y método HTTParty:

class Token
  require 'httparty'

  include HTTParty
  base_uri 'https://<some url>'
  headers 'auth_user' => 'user'
  headers 'auth_pass' => 'password'
  headers 'auth_appkey' => 'app_key'

  def self.getToken
    response = get('/auth/token')
    @token = response['auth']['token']
  end
end

sé que funciona porque puedo llamar al método en la consola Rails y recuperar un token con éxito.

¿Cómo puedo probar el código anterior en RSpec?

Mi intento inicial no funciona:

describe Token do
  before do
    HTTParty.base_uri 'https://<some url>'
    HTTParty.headers 'auth_user' => 'user'
    HTTParty.headers 'auth_pass' => 'password'
    HTTParty.headers 'auth_appkey' => 'app_key'
  end

  it "gets a token" do
    HTTParty.get('auth/authenticate')
    response['auth']['token'].should_not be_nil
  end
end

Dice: NoMethodError: undefined method 'base_uri' for HTTParty:Module...

¡Gracias!

¿Fue útil?

Solución

Dado que está probando un módulo, puede intentar algo como esto:

describe Token do
   before do
      @a_class = Class.new do
         include HTTParty
         base_uri 'https://<some url>'
         headers 'auth_user' => 'user'
         headers 'auth_pass' => 'password'
         headers 'auth_appkey' => 'app_key'
      end
   end

   it "gets a token" do
      response = @a_class.get('auth/authenticate')
      response['auth']['token'].should_not be_nil
   end
end

Esto crea una clase anónima y la extiende con HTTPpartyLos métodos de clase.Sin embargo, no estoy seguro de que la respuesta sea tal como la tiene.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top