How to write an RSpec controller test

Premise

・ Devise is introduced ・ About the contents of the test written this time -If the user is logged in, it should be displayed correctly.

app/controllers/posts_controller.rb


  class PostsController < ApplicationController
    before_action: authenticate_user!

    def index
      @posts = Post.all
    end
  end

About file creation / role

① Create requests and factories folders under spec

-Create spec/requests/posts_request.rb → File that describes the contents of the test

・ Spec/factories/user.rb ・ Create spec/factories/post.rb → File to create dummy data about user and post

(2) Create a support folder and factory_bot.rb file under spec so that FactoryBot can be used.

spec/support/factroy_bot.rb


RSpec.configure do |config|
  config.include FactoryBot::Syntax::Method #For FactoryBot
  config.include Devise::Test::IntegrationHelper, type: :request #devise sign_in method becomes available
end

Then edit rails_helper.rb

spec/rails_helper.rb


require 'spec_helper'
...
require 'rspec/rails'
require 'support/factroy_bot'  #← This description allows you to use FactroyBot

Creating dummy data

spec/factories/user.rb


FactoryBot.define do
  factory :user do 
    email { Faker::Internet.email }
    name { "testuser1" }
    password { "password" }
    password_confirmation { "password" }
  end 
end

spec/factories/post.rb


FactroyBot.defind do
  factory :post do
    body { Faker::Lorem.charactors(number: 15) } #Create a 15-character dummy statement
  end
end

Creating test code

spec/requests/post_spec.rb


require 'rails_helper'

RSpec.describe 'post_controller test', type: :request do
  let(:user) { create(:user) }
  let(:post) { create(:post) }
  describe 'If you are logged in' do
    before do
      sign_in user
      get posts_path
    end

    it 'Requests to the post list page should be 200 ok' do
      expect(response.status).to eq 200
    end

    it 'The page title is displayed correctly' do
      expect(response.body).to include('Post list')
    end
  end

  describe "If you are not logged in" do
    before do 
      get posts_path
    end
    
   it 'The request will be 401' do
     expect(response.status).to eq 401
   end
  
   it 'Title is not displayed' do
     expect(response.body).to_not include("Post list")
   end
  end
end

All you have to do is run RSpec!

$ rspec spec/requests

Finally###

Since this is a beginner's article, I think there are ninety-nine mistakes. If anyone notices it, I would be very grateful if you could let me know! !

Recommended Posts

How to write an RSpec controller test
[SpringBoot] How to write a controller test
[RSpec on Rails] How to write test code for beginners by beginners
Introduction to RSpec 1. Test, RSpec
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to write an if statement to improve readability-java
How to use "sign_in" in integration test (RSpec)
JUnit 5: How to write test cases in enum
How to write test code with Basic authentication
How to create an application
How to write good code
How to write a unit test for Spring Boot 2
Introduction to RSpec 5. Controller specs
Bit Tetris (how to write)
How to write java comments
How to write to apply gem Pagy (pagination) to an array
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails seed
[Ruby] How to write blocks
How to write an external reference key in FactoryBot
How to handle an instance
[RSpec] How to test error messages set by Shoulda-Matchers
How to dynamically write iterative test cases using test / unit (Test :: Unit)
[RSpec] Use WebMock to test logic using an external API
How to test a private method with RSpec for yourself
How to erase test image after running Rspec test with CarrierWave
[Integration test code] How to select an element from date_select
How to unit test Spring AOP
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
Baseball ball count (how to write)
How to insert an external library
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
How to crop an image with libGDX
Introduce RSpec and write unit test code
How to filter JUnit Test in Gradle
How to blur an image (super easy)
[Basic] How to write a Dockerfile Self-learning ②
Summary of how to write annotation arguments
[Note] How to get started with Rspec
How to publish an application on Heroku
[Swift] How to use Tab Bar Controller
[Java] How to output and write files!
[Ruby on Rails] Controller test with RSpec
Write code that is difficult to test
How to test private scope with JUnit
How to write Spring AOP pointcut specifier
How to write and explain Dockerfile, docker-compose
[For Rails beginners] Summary of how to use RSpec (get an overview)
Rails: How to write a rake task nicely