[RUBY] Use webmock with Rspec

Overview

A gem that allows you to define a Mock for API response when testing the ability to access external APIs.

Added webmock to Gemfile

group :test do
  gem 'webmock'
end
$ bundle install --path vendor/bundle

Model to be tested

model/smaple.rb


require 'net/https'
require 'uri'

class Sample
  include ActiveModel::Model

  def get_request(api_url, params)
    uri = URI(api_url)
    uri.query = params
    request(uri)
  end
end

make rspec

When the above get_request method is executed, the contents of the stub defined in WebMock are returned.

require 'rails_helper'
require 'webmock/rspec'

RSpec.describe Abyss, type: :model do
  describe 'facet', type: :facet do
    before do
      WebMock.enable!
      WebMock.stub_request(:any, "www.example.com").to_return(
        body: "This is a mock",
        status: 200,
        headers: { 'Content-Length' => 7 }
      )
    end
    subject { Sample.new }
    it 'debug' do
      res = subject.send(:get_request, 'http://www.example.com', '')
      puts res.body # This is a mock
    end
  end
end

reference

-Stub API access using WebMock in Rails development

Recommended Posts

Use webmock with Rspec
Use ProGuard with Gradle
Testing model with RSpec
Use Puphpeteer with Docker
Use ngrok with Docker
[Rails] Test with RSpec
Test Nokogiri with Rspec.
Mock and stub with minitest (use RR, WebMock, MiniTest :: Mock)
Use WebJars with Gradle
Use jlink with gradle
Use include matcher obediently when checking hash with RSpec
Use Lambda Layers with Java
Use GDAL with Python with Docker
Check CSV value with RSpec
Use pfx certificate with Okhttp3
Test Active Strage with RSpec
Use multiple databases with Rails 6.0
Test GraphQL resolver with rspec
Use Spring JDBC with Spring Boot
Use Ruby with Google Colab
Use SpatiaLite with Java / JDBC
Use log4j2 with YAML + Gradle
[Docker] Use whenever with Docker + Rails
[RSpec] Use WebMock to test logic using an external API
Use PlantUML with Visual Studio Code
Use Basic Authentication with Spring Boot
Use java with MSYS and Cygwin
Use constructor with arguments in cucumber-picocontainer
Use PostgreSQL inet type with DbUnit
Why use orchestration tools with Docker
Test with RSpec + Capybara + selenium + chromedriver
Use bootstrap 4 with PlayFramework 2.6 (no CDN)
Let's use Amazon Textract with Ruby
Use Git with SourceTree and Eclipse
Use JDBC with Java and Scala.
Use Java 11 with Google Cloud Functions
How to use mssql-tools with alpine
Copy and paste test with RSpec
Beginning with Spring Boot 0. Use Spring CLI
Use cuda11.0 with pytorch using Docker
Let's unit test with [rails] Rspec!
[Ruby on Rails] View test with RSpec
[Rails] How to use rails console with docker
Use Firebase Realtime Database with Cocos Creator.
I want to use DBViewer with Eclipse 2018-12! !!
[Note] How to get started with Rspec
Use Symbolic Link with Docker multi-stage builds
[Ruby on Rails] Controller test with RSpec
Use FacesContext as a Mock with PowerMockito
[JaCoCo (Java Code Coverage)] Use with NetBeans
Use cljstyle with Spacemacs on Ubuntu on WSL2
Use Spring Security JSP tags with FreeMarker
[Ruby on Rails] Model test with RSpec
Use a named format with Ruby's format method
Use cache with EhCashe 2.x with Spring Boot
Use ZStandard with .NET Core + Docker (Alpine)