Introduction to RSpec 2. RSpec setup

Continuation of Last time.

RSpec installation and configuration

First, let's get ready to use RSpec.

Gem installation

Add ** rspec-rails ** to Gemfile and install.

Gemfile


group :development, :test do
  gem 'rspec-rails'
end
$ bundle install

Test DB creation

Check the test DB. If the following is not set, add it.

config/database.yml


test:
  <<: *default
  database: projects_test  #If you are using MySQL or PostgreSQL
  database: db/test.sqlite3  #If you are using SQLite

Creating a DB.

$ rails db:create:all

RSpec installation

$ rails generate rspec:install

For now, RSpec setup is complete. thank you for your hard work! This command will generate the following file directories:

File function
.rspec setting file
spec Directory to put spec files
spec/spec_helper.rb Helper file used in the test
spec/rails_helper.rb Helper file used in the test

Settings in .rspec

Next, let's set up RSpec.

.rspec


--require spec_helper  #spec_See helper
--format documentation # Make the test results easy to read
--warnings # Display all warnings (not an error, but will tell you what has been deprecated)

--warnings is omitted this time to eliminate information noise when running the test.

Other settings

First, install binstub to speed up the test startup time.

Gemfile


group :development do
  gem 'spring-commands-rspec'
end
$ bundle install

Next, set which spec file is automatically generated when the rails generate command is executed.

config/application.rb


config.generators do |g|
  g.test_framework :rspec,
  fixtures: false,
  view_specs: false,
  helper_specs: false,
  routing_specs: false
end

The spec file set to false will not be automatically generated, and only the model and controller spec files will be automatically generated. You can also manually generate the spec file without automatic generation later.

Run the test

If RSpec is installed correctly, you can run the test with the following command.

$ bin/rspec

Continued

Recommended Posts

Introduction to RSpec 2. RSpec setup
Introduction to RSpec 1. Test, RSpec
RSpec setup
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
RSpec Setup
Rspec introduction memo_Rails
Introduction to SWING
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to JAR files
Introduction to Ratpack (8)-Session
Introduction to Ratpack (6) --Promise
Set RSpec to DRY
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to javadoc command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to Keycloak development
Introduction to javac command
Introduction to RSpec 4. Create test data with Factory Bot
Introduction to Design Patterns (Builder)
Introduction to Android application development
Introduction to Ratpack (5) --Json & Registry
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
[Ruby] Introduction to Ruby Error statement
Introduction to EHRbase 2-REST API
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)
Introduction to Spring Boot Part 1
Introduction to Ratpack (1) --What is Ratpack?
XVim2 introduction memo to Xcode12.3
Introduction to RSpec-Everyday Rails Summary-
Introduction to Design Patterns (Strategy)
[Introduction to rock-paper-scissors games] Java
Introduction to Linux Container / Docker (Part 1)
Introduction to swift practice output Chapter5
[Introduction to Java] About lambda expressions
[Introduction to Java] About Stream API
Introduction to Functional Programming (Java, Javascript)
Introduction to algorithms with java-Shakutori method