私は、次の単純なクラスとHTTParty方法があります:私はRailsのコンソールでメソッドを呼び出し、成功したバックトークンを取得することができますので、それがの作品を知っているRSpecでこのコードをテストするには?
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
を。
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
...
をありがとう!
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
のクラスメソッドでそれを拡張する:あなたは、モジュールをテストしているので
サーバー、または(非常に薄い)クライアントをテストしますか? –
クライアントをテストします。これは私がWebサービスを消費するために書いた最初のメソッドです。私はそれを追加する前にテストを書こうと思っていますが、使用する構文は分かりません。 –