title: ruby (rails) Net::HTTPResponse undefined method closed?
body: <p>Ruby-on-Rails, Net::HTTPResponse
  NoMethodError:undefined method `closed?' for nil:NilClass</p>
  
  <p>Hello, I have faced an issue:
  I use the RSpec framework and I need to mock a response within a method which I test.</p>
  
  <p>here is a sample of the method:</p>
  
  <pre><code>def perform_request
      response = DataClient.new.request(arg: 'some_arg')
      # do some thing with the response
  end 
  
  RSpec file:
      describe '#perform_request'
        let(:response) do
          Net::HTTPResponse.new(1.0, 200, "OK")
        end
  
        let(:double_response) {double('DataClient', request: response)}
  
        before do
         response.body = {some_key:some_value}.to_json
         allow(DataClient).to receive(:new).and_return(double_response)   
        end
  
       it 'returns a response body' do
         MyClass.new.perform_request
         expect(response.body).to eq({some_key:some_value}.to_json)
       end 
     end
  </code></pre>
  
  <p>the problem happens when I try to call the 'body' method for the Net::HTTPResponse instance to read the body of it.</p>
  
  <p>It throws the NoMethodError with the message  "undefined method `closed?' for nil:NilClass" which I have no idea about.</p>
  
  <p>I have tried to search for any explanation with question variations such as 'Net::HTTPResponse body undefined method <code>closed?'', 'Ruby Net:: HTTPResponse body undefined method</code>closed?', 'Ruby undefined method `closed?' but couldn't find any relevant answer.  </p>
  
  <p>In advance, I have to say I can't use any other client library except the Net:: HTTP without any wrappers. Please don't advice it.   </p>
  
  <p>The point of the question is not just to get an explanation but also to get advice how to solve the problem.
  Thank you in advance for any explanation and advice. </p>