首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用webmock模拟请求?

如何使用webmock模拟请求?
EN

Stack Overflow用户
提问于 2020-03-12 19:12:52
回答 1查看 474关注 0票数 0

我的应用程序使用API创建了一个github gist。我需要用rspec模拟对API的请求。我正在使用webmock gem,但我不太明白如何在我的应用程序中使用它。我需要一点帮助才能开始。

这是我的spec/Git_Request_spec.rb

代码语言:javascript
复制
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

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2020-03-13 12:06:20

您需要使用存根方法来模拟您与api的交互。

代码语言:javascript
复制
stub_request(:http_method, url).with(`your data in 
request`).to_return(`what do you expect to receive in response`).
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60660516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档