0

次の単純なクラスと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

Railsコンソールでメソッドを呼び出してトークンを正常に取り戻すことができるので、機能することはわかっています。

上記のコードをRSpecでテストするにはどうすればよいですか?

それで私の最初の刺し傷は機能しません:

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

それは言う:NoMethodError: undefined method 'base_uri' for HTTParty:Module..。

ありがとう!

4

1 に答える 1

1

モジュールをテストしているので、次のようなことを試してみてください。

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

これにより、匿名クラスが作成され、HTTPpartyのクラスメソッドで拡張されます。ただし、応答が返ってくるかどうかはわかりません。

于 2012-03-09T21:48:36.240 に答える