私はAPIクライアントを構築するためにRestClient宝石を使用していますし、APIへの呼び出しが、ここで、この方法で処理されるRestClient :: Request.executeのRSpecテスト:リクエストを見る方法はありますか?
def call(api_name,api_endpoint,token = nil,*extra_params)
endpoint = fetch_endpoint(api_name,api_endpoint)
params = {}
endpoint['params'].each_with_index { |p,i| params[p] = endpoint['values'][i] }
puts params
if token.nil? then
response = RestClient::Request.execute(method: endpoint['method'], url: endpoint['url'], params: params.to_json)
else
response = RestClient::Request.execute(method: endpoint['method'], url: endpoint['url'], headers: {"Authorization" => "Bearer #{token}"}, params: params.to_json)
end
response
end
ご覧の通り、私はすべてのコールのパラメータ/値を持つハッシュを取り付けています応答を得るにはRestClient::Request#execute
を呼び出してください。
この1
it 'request_autorization' do
obj = SpotifyIntegration.new
response = obj.call('spotify','request_authorization',nil,state: obj.random_state_string)
myjson = JSON.parse(response.body)
expect(myjson).to eq({})
end
のような私のテストのいくつかは、400 Bad request
エラーを返すされている、と私は本当に理由を知らないということが起こります。このような他のテストは、同じ方法で処理されていますが、これと同じように処理されます。
it 'my_playlists (with no authorization token)' do
obj = SpotifyIntegration.new
expect {
response = obj.call('spotify','my_playlists')
}.to raise_error(RestClient::Unauthorized,'401 Unauthorized')
end
要求の送信方法はありますか?私は、RestClientがマウントされている/私の要求を対応するAPIに送信する方法を見ていますか?このように私は何が起こっているのか理解できました。私はRestClientのドキュメントを検索し、類似した何も見つからなかったが、多分あなたのいくつかが行う方法を知ってました
puts RestClient::Request.prepared_request
または
puts RestClient::Request.prepared_url
のようなものを意味する「要求を参照してください」と
この。
'.execute'の代わりに' .new'を使うと、あなたは何かできることがありますか?ドキュメント[ここ](http://www.rubydoc.info/gems/rest-client/RestClient/Request#initialize-instance_method)に基づいて推測するだけです –
私はチェックするつもりです。確かにいくつかの可能性があります。ありがとう! –
'RestClient.log'を使用して詳細情報を取得するか、WebMockを使用して、期待どおりに生成されたWebリクエストが表示されていることをテストしてみることができます。 – alberge