Webmock provides a suggested stub for a Rails controller action.
I have not found proper syntax required for the return body.
The request (abbreviated)
intent = Stripe::PaymentIntent.create({ amount: authorisation.to_i, currency: 'eur', [...] { idempotency_key: @idempotency_key }, ) {client_secret: intent.client_secret}.to_json
The suggestion:
stub_request(:post, "https://api.[...]payment"). with( body: {"amount"=>"250", "capture_method"=>"manual", "currency"=>"eur", "description"=>" user_747712275", "payment_method_types"=>["card"], "statement_descriptor_suffix"=>"full user_747712275"}, headers: { 'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer nil', [...] }). to_return(status: 200, body: "", headers: {})
the response is a long JSON hash, of which - for the time being - the interest remains on the client_secret
as above:
{ "id": "3RFaceKOXN2EnLzd0yjHmzkd", "object": "payment", "amount": 250, [...] "client_secret": "pi_3mRFaceKOXN2EnLzd0yjHzkd_secret_if8u6njASlMQ8XJ0bAAvgNo4q", [...] "transfer_data": null, "transfer_group": null }
no quotes matches the documentation indications for JSON and XML responses, but in that case minitest complains that NameError: undefined local variable or method 'null' for an instance of ApiControllerTest
, where null is the API's database value which conflicts with ruby's nil
.
At this point, the assumption in order to continue the test to completion of action:
• is it correct to have the hash syntax directly body: { "id": "OXN2EnLzd0yjHmzkd"..
• replace null
with nil
(any other ruby replacements needed?)
However, this then generates error:WebMock::Response::InvalidBody: must be one of: [Proc, IO, Pathname, String, Array], but you've used a Hash. Please convert it by calling .to_json .to_xml, or otherwise convert it to a string.
which the documentation does not contemplate.
Setting [...] "transfer_group": null }
to [...] "transfer_group": nil }.to_json
returns error of registered request stubs: [...]
with no to_return(status: [...]
. In other words the stub has no return.
How should the syntax be cast to have the return available for processing?
allow(something).to receive(:something).and_return(some_hash)