[Rails] About Rspec response test

Introduction

Overview

I tried to summarize how to write test code in Rspec. (It will be a response test edition.)

I learned by referring to the following articles. Create a template for initial RSpec settings in Rails-Qiita Test Rails tutorials (Chapter 3, Chapter 4, Chapter 5) with RSpec-Qiita

1. Rspec setup

Install Gem

Add Rspec to gemfile

gemfile


group :development, :test do
  gem 'rspec-rails', '~> 4.0.1'
end

Then install Gem.

$ bundle install

Install rspec in your project with the rails command.

$ rails generate rspec:install

Edit rspec settings

.rspec


--require rails_helper
--format documentation

config/application.rb


module RailsTutorial
  class Application < Rails::Application
    config.load_defaults 5.2
    
    #↓ Add
    #Rails g by specifying rspec in the test framework~Settings that automatically create a spec file when you execute
    config.generators do |g|
      g.test_framework :rspec,
                       helper_specs: false,
                       routing_specs: false,
                       view_specs: false,
                       controller_specs: false
    end
    #↑ Add

  end
end


2. Write a test with Rspec

Create an Rspec file

It seems that the test code is called'spec'in Rspec. (The controller test code seems to be like'controller spec'.)

For Rails, you can create a file with the rails generate command.

#For controller
$ rails g rspec:controller file name

#For model
$ rails g rspec:model file name

$ rails g rspec:controller StaticPages

Write test code

spec/requests/static_pages_request_spec.rb


require 'rails_helper'

RSpec.describe 'Access to static_pages', type: :request do

	#Request sending test to home page
	context 'GET #home' do
		before { get static_pages_home_path }

		#Response test to request
		it 'responds successfully' do
			expect(response).to have_http_status 200
		end
	end

end

I'm not sure about describe or context.

Let's look at each one.

・ Describe, type

_spec.rb



RSpec.describe 'Access to static_pages', type: :request do

end

↓

RSpec.describe [Test name], type: [Spec type] do

end

#Spec type
#This time it is a response test, so it will be a request.
#Other than that'system','controller','model','routing','view'And so on.

・ Context, before, it

_spec.rb


RSpec.describe 'Access to static_pages', type: :request do
    context 'GET #home' do
        before { get static_pages_home_path }

        #Response test to request
        it 'responds successfully' do
            expect(response).to have_http_status 200
        end
    end
end

  ↓

RSpec.describe 'Access to static_pages', type: :request do
    context ['~in the case of'(With your favorite name)] do
        before [Advance preparation]

        #Response test to request
        it ['Contents of specifications'(With your favorite name)] do
            [Expected behavior]
        end
    end
end

If you read in order from the top The test code is finally listed in it. The test code is written in a structure that nests many describe> context> ....

spec/requests/static_pages_request_spec.rb


require 'rails_helper'

RSpec.describe 'Access to static_pages', type: :request do

	#Request sending test to home page
	context 'GET #home' do
		before { get static_pages_home_path }

		#Response test to request
		it 'responds successfully' do
			expect(response).to have_http_status 200
		end
	end

end

expect(response).to have_http_status 200

This is before and {get static_pages_home_path} When the GET method is used to communicate with the home path, if the response returned is 200 (OK), it means that the test is OK.

That's easy, but it's about how to write an Rspec response test in Rails.

Recommended Posts

[Rails] About Rspec response test
About RSpec (Rails)
[Rails] Test with RSpec
[Rails] Test code using Rspec
About Rails 6
[Rails5] Rspec -Unit test when nesting-
About app testing RSpec (unit test)
Let's unit test with [rails] Rspec!
About Rails routing
[Ruby on Rails] View test with RSpec
Rails tutorial test
[Ruby on Rails] Controller test with RSpec
[Rails] About ActiveJob ,!
Rspec Basics [Rails]
[Ruby on Rails] Model test with RSpec
[Rails5] Rspec -validation-
About Rails controller
I rewrote the Rails tutorial test with RSpec
Introducing Rspec, a Ruby on Rails test framework
[Rails] About migration files
[Rails 6] About main gems
[Rails] About active hash
About rails application server
Introduction to RSpec 1. Test, RSpec
About rails kaminari pagination
Test run on rails
About rails version specification
[Rails] About Slim notation
Test Nokogiri with Rspec.
Rails, RSpec installation procedure
[rails] About devise defaults
Rails: About partial templates
About rails strong parameters
[Beginner] About Rails Session
[Rails] From test preparation to model unit testing [RSpec]
[Rails] Test of star evaluation function using Raty [Rspec]
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
About method matchers used in model unit test code (RSpec)
rails test db only drop
about the where method (rails)
[RSpec] Let's use FactoryBot [Rails]
[Ruby on Rails] about has_secure_password
About naming Rails model methods
[Rails] About scss folder structure
[Challenge CircleCI from 0] Build an automated test (Rails6.0, mysql8.0, Rspec)
About Rails scraping method Mechanize
[RSpec on Rails] How to write test code for beginners by beginners
Understand code coverage with Rspec, the Ruby on Rails test framework
Image upload using CarrierWave ~ Rspec test ~
[Rails] About the Punk List function
About the symbol <%%> in Rails erb
Test with RSpec + Capybara + selenium + chromedriver
RSpec ~ Task model validation test creation
[Rails] About implementation of like function
[Rails] RSpec Kihon no Ki [Introduction]
[Rails] About helper method form_with [Basic]
About RSpec introduction & closely related gems
Copy and paste test with RSpec
[RSpec] How to write test code
Consideration about Rails and Clean Architecture
[RSpec] Unit test (using gem: factory_bot)