0

I am trying to simulate a POST request with rspec. I thought about using the webmock gem, after searching how it worked, my code looks like this:

Gist_Request_spec.rb:

require_relative '../Gist_Request.rb'
require 'spec_helper'
require 'webmock'
include WebMock::API

WebMock.enable!

RSpec.describe GistRequest do
  describe "#post" do
    it "crear gist" do
      stub_request(:post, "https://api.github.com/gists").
      with(
        body: {"description" => "description","public" => true,"files" => { "file.txt" => { "content" => "content" }}},
        headers: {
       'Accept'=>'*/*',
       'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
       'Authorization'=>'Basic Sm9lbEFsYXlvbjEyMzo0NzVmYjI1ZWU0MGQyNGQzZGM1NWQ0MzQ5YjI4MTU3MjM1ZjdmZDBk',
       'Host'=>'api.github.com',
       'User-Agent'=>'Ruby'}).to_return(status: 201, body: "", headers: {})

      expect(WebMock).to have_requested(:post, "https://api.github.com/gists").with { |req| req.status == 201 }
    end
  end
end

I added code to my file spec_helper.rb and now it looks like this:

The first lines spec_helper.rb:

require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)

But when I run rspec spec/Gist_Request_spec.rb, it returns the following error:

GistRequest
  #post
    crear gist (FAILED - 1)

Failures:

  1) GistRequest#post crear gist
     Failure/Error: expect(WebMock).to have_requested(:post, "https://api.github.com/gists").with { |req| req.status == 201 }

       The request POST https://api.github.com/gists with given block was expected to execute 1 time but it executed 0 times

       The following requests were made:

       No requests were made.
       ============================================================
     # ./spec/Gist_Request_spec.rb:21:in `block (3 levels) in <top (required)>'
Cœur
  • 37,241
  • 25
  • 195
  • 267

0 Answers0