Error WebMock::NetConnectNotAllowedError in testing with stub using minitest in rails (using Faraday)

41 Views Asked by At

I'm trying to create a test to check the correct behaviout of my service object,

def api_call_post(connection, message, url)
    pp message
    response = connection.post do |conn|
      conn.url url
      conn.body = message
      conn.headers = @headers
    end
    check_response(response)
  end
...

this is the test :

test "create a title" do

   body = { "name" => 'some name',
                "external_id" =>'004',
                "title_type" =>'feature',
                "tags" => 'some tag' }.to_json

    puts body

    stub_request(:post, "some web")
      .with(body: body )
      .to_return(status: 201, body: "Created", headers: {})

    response = MovidaApi.new(payload).create_title

    assert response.success?
    assert_equal "Created", response.body
  end
...

The problem comes when I include the .with in the stub, (without it's working ok), I printed statements and the output are exactly the same.

The error is:

Error: MovidaApiTest#test_create_a_title: WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: POST some web with headers {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}

What i'm doing wrong?

I tried to use the snippet suggested but still don't work.

I expect the test to pass

0

There are 0 best solutions below