ruby on rails - How to stub HTTP POST request in rspec? -


i have controller contains method , conditional statements. following sample of controller.

class <controllername> < applicationcontroller def method   if params["c"]    http = net::http.new(uri.host, uri.port)    req = net::http::post.new("api_url_here")    response = http.request(req)    array = json.parse(response.body)    url = params["s"]    .....   elsif params["e"]    .....   else    .....   end end end 

i wrote rspec above controller

it "should something"    array ="some array values"    :method, {"c" => "value c", "s" => "value s"}    expect(response).to have_http_status(200) end 

i know above rspec method wrong. when case runs value array , response obtained post call inside method , response httpbadrequest expected.

what want is

to stub values array , response in spec case(these values needed later operations) , spec case not call http::post inside method

you can mock answer

expect_any_instance_of(net::http::post).to receive(:request) { response_data_here } 

you can use vcr: https://github.com/vcr/vcr

when run vcr first time, real request , save response fixture, second time use fixture , avoid real http request


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -