[RUBY] Hello RSpec

I'm a new Rails engineer. I learned RSpec when testing Rails, so I summarized it. We are serializing an engineer who will become one serving in 100 days. Today is the 7th day By the way, for a comparison between Minitest and RSpec here

What is RSpec

A framework using a domain-specific language (DSL) for testing classes and methods created in Ruby and Ruby on Rails. In other words, it is a test-only programming language.

FactoryBot A gem that supports the creation of data for testing Factory Bot makes it easy to prepare test data There is also a mechanism called Fixture in the Rails standard, but It is easier to grasp the state of data and the relationship between data than here. So this time I also use this.

Capybara A framework for E2E (End-to-End) testing of web applications. It is used in combination with RSpec etc. You can simulate the browser operation of a Web application.

Basic form of RSpec

The basic form of RSpec description is as follows.

describe [Target to describe the specification(Test target)], type[type of spec] do
  context[A situation] do
    before do
      [Advance preparation]
    end
    it[Specifications (Summary of expectations)] do
      [Expected behavior]
    end
  end
end

It's hard to understand even if you look at this, so I'll divide it. describe

describe [Target to describe the specification(Test target)], type[type of spec] do

end

In describe, write what you are trying to describe the specification for. For example, if you want to write a specification for the system test and profile display function

describe 'Profile display function', type: :system do

That's how it was said.

context This is used to classify the contents of the test according to the situation and the variation of the state. For example, whether the user's input is correct or incorrect, whether the user is logged in, etc. I will describe it in context.

before before writes the code to implement the prerequisites for the entire area The following are prerequisites for viewing your profile. In this case, it is a prerequisite that you are logged in.

context 'When the user is logged in' do
  before do
    visit login_path
    fill_in 'mail address', with: '[email protected]'
    fill_in 'password', with: 'password'
    click_button 'Login'
  end
end

it The last it describes the expected behavior and text in the code inside the block expect(page).to have_content 'hoge' The above description has the content "hoge" on the screen, right? ?? It is the content. Since it is expect to have content, it is up to that point if it remains in English.

#On the profile screen'User profile'There is content??
it 'The user's profile is displayed' do
  expect(page).to have_content 'User profile'
end

To summarize the above


describe 'Profile display function', type: :system do
  context 'When the user is logged in' do
    before do
      visit login_path
      fill_in 'mail address', with: '[email protected]'
      fill_in 'password', with: 'password'
      click_button 'Login'
    end
    it 'The user's profile is displayed' do
      expect(page).to have_content 'User profile'
    end
  end
end

The basic form of RSpec is established in this way. It's hard to understand everything suddenly, but when I divide it, it seems quite so.

This time, I summarized it in terms of how to see the basic form. It actually looks more nested and complicated, First of all, I would like to hold down the basic form and deepen my understanding of RSpec.

That's all for today ** 93 days to become a full-fledged engineer **

Recommended Posts

Hello RSpec
RSpec setup
Introducing RSpec
RSpec preparation
Install RSpec
RSpec Setup
RSpec Basics
Rspec, TDD (1)
Rspec introduction memo_Rails
Java, Hello, world!
Bilingual Hello World.py.rb
[Swift] Display Hello Swift!
Java Hello World
Hello Java Lambda
Rspec Basics [Rails]
[Rails5] Rspec -validation-
About RSpec (Rails)