[RUBY] Rspec introduction memo_Rails

Technical memo (introduction) in RSpec learning

What is RSpec

It is a testing framework for BDD (Behavior Driven Development) in ruby.

What is capybara

In addition to being able to simulate the browser operation of a Web application, the browser operation can be intuitively described using capybara's DSL. You can actually operate a browser or a Handless browser (without GUI)

What is Factory Bot?

It is a gem that supports the creation of test data. Test data can be easily prepared and can be called from the test and used.

Introductory gem

Introducing RSpec

Add gem to Gemfile At this time, fill in the block of the test group By filling in the group, the gem will be enabled only in the test environment.

Gemfile


group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'webdrivers'
  gem 'rspec-rails'
end

python


$ bundle

After executing the bundle, execute the following generate command to create the directories and configuration files required for RSpec.

python


$ bin/rails g rspec:install

After this, the test directory prepared by Rails by default is unnecessary, so delete it.

python


$ rm -r ./test

Capybara initial settings

Set up Handless Chrome in your browser. At this time, an error will occur and can be resolved by referring to the link below. https://takuyakou.hatenablog.com/entry/2019/09/12/192755

spec_helper.rb


RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :selenium_chrome_headless
  end
  ...abridgement
end

Factory Bot installation

Add factory_bot_rails to Gemfile and run bundle.

Gemfile


group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'factory_bot_rails'
end

Now you can test from the spec directory that was created without Rails App.

Recommended Posts

Rspec introduction memo_Rails
Introduction to RSpec 1. Test, RSpec
Introduction to RSpec 2. RSpec setup
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to RSpec 3. Model specs
Rails book review app RSpec introduction
[Rails] RSpec Kihon no Ki [Introduction]
About RSpec introduction & closely related gems
Lombok ① Introduction
RSpec setup
Introducing RSpec
RSpec preparation
Install RSpec
RSpec Setup
RSpec Basics
Rspec, TDD (1)
[Java] Introduction
Introduction (editing)
Hello RSpec
[Ruby on Rails] Until the introduction of RSpec
I will not hesitate anymore! RSpec introduction flow
Introduction of Rspec and Japanese localization of error messages