我的应用程序使用API创建github gist。我需要用rspec模拟对API的请求。我正在使用webmock-gem,但我不太明白如何将其用于我的应用程序。我需要一点帮助才能开始。
这是我的spec/Git_Requestrongpec.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
有什么想法吗?
您需要使用方法stub_request来模拟您与api 的交互
stub_request(:http_method, url).with(`your data in
request`).to_return(`what do you expect to receive in response`).