前回の応用編。
環境
webmock (3.4.2)
実例
想定する用件
"https://example.com/hogehoge"
というURLのに
{ "id": 100 }
のようなJSONを作り、idが100だったら成功するが、0を指定すると500エラーで落ちる。みたいなAPIを想定した場合
実際の設定
以下のように withで条件を設定して、そのときに出てくるレスポンスとしておいておくとよい
#成功時 stub_request(:post, "https://example.com/hogehoge"). with( body: "{\"id\":100}",). to_return(status: 200, body: {success: true}.to_json, headers: {}) # 失敗時( stub_request(:post, helium_user_url). with( body: "{\"id\":0}",). to_return(status: 500, body: "", headers: {})