Как смоделировать POST-запрос в Ruby с гемом webmock? - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь смоделировать запрос POST с помощью rspe c. Я думал об использовании гема webmock, после поиска того, как он работает, мой код выглядит так:

Gist_Request_spe c .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

Я добавил код в свой файл spec_helper.rb и теперь он выглядит так:

Первые строки spec_helper.rb :

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

Но когда я запускаю rspe c spec / Gist_Request_spe c .rb , он возвращает следующую ошибку :

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)>'
...