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