Domanda

Ho il seguente metodo di classe semplice e 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
.

so che funziona perché posso chiamare il metodo nella console dei binari e ottenere un token indietro.

Come posso testare il codice sopra in RSPEC?

La mia pugnalata iniziale non funziona:

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 ...

Grazie!

È stato utile?

Soluzione

Dato che stai testando un modulo potresti provare qualcosa del genere:

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
.

Ciò crea una classe anonima e lo estende con i metodi di classe di HTTPparty.Tuttavia, non sono sicuro che la risposta tornerà come ce l'hai.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top