0

My app create a github gist using API. I need to simulate a request to the API with rspec. I'm using the webmock gem but I don't quite understand how to use it for my application. I need a little help to get started.

This is my spec/Git_Request_spec.rb

require_relative '../Gist_Request.rb'
require 'spec_helper'

RSpec.describe GistRequest do
  describe "#post" do
    it "crear gist" do
      filename = "test.txt"
      description = "descripción"
      state = true
      content = "contenido"

      gist_create = GistRequest.new(description, state, filename, content)
      gist_create.post()

      expect(gist_create.response_status).to eq "201"
    end

    it "campos no válidos" do
      filename = "test.txt"
      description = "descripción"
      state = true
      content = "contenido"

      gist_create = GistRequest.new(filename, content, state, description)
      gist_create.post()

      expect(gist_create.response_status).to eq "422"
   end
 end
end

Any ideas?

  • Once you have called `Webmock.enable!` (see https://github.com/bblimke/webmock), whenever you make a request from your test Webmock will cause it to error, and will out instructions for how to mock that request. – max pleaner Mar 12 '20 at 20:22
  • Should I call him in Gist_Request_spec.rb? – Joel Alayon Mar 12 '20 at 20:32

1 Answers1

0

You need to use the method stub_request to simulate your interaction with api

stub_request(:http_method, url).with(`your data in 
request`).to_return(`what do you expect to receive in response`).
  • Like this? ```stub_request(:http_method, url).with(gist_create.response_status).to_return("201")``` that didnt work for me: ```WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: POST .......``` – Joel Alayon Mar 17 '20 at 17:34