There are two types of tests: unit tests and integration tests.
-Unit test A test to see if it works properly for one program. (Example) For each model class
-Integration testing 1 Test for lotus processing. (Example) Flow from inputting and sending a value from the user's new registration screen until the record is added to the database
"Rspec" is a jem for testing, "Factory_bot" creates information temporarily when testing It is a help tool.
Therefore, let's install them all at once.
①jemno Installation
Gemfile
group :development, :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
end
Terminal
bundle install
(2) Install the minimum required file / directory structure of RSpec in Rails
Terminal
$ rails g rspec:install
#> create .rspec #RSpec configuration file
#> create spec #Store specs
#> create spec/spec_helper.rb #Helper for spec description
#> create spec/rails_helper.rb #Helper for Rails-specific spec description
Add the required directories and files here.
③ Create necessary files Here is a validation test for the users model I will go.
◆ Test file The files in spec / are 1 for the rb file under test under app / Arrange in a one-to-one correspondence.
The specs for app / models / user.rb It will be spec / models / user_spec.rb.
◆ Data file for testing (factory_bot) By placing the factory in spec / factories You can easily use the test data.
④ File description
Set so that the namespace can be omitted.
spec/rails_helper.rb
RSpec.configure do |config|
+ config.include FactoryGirl::Syntax::Methods
end
.rspec
--format documentation
--require spec_helper
spec/models/user_spec.rb
spec/factories/users.rb
FactoryBot.define do
factory :user do
nickname {"taro"}
email {"[email protected]"}
password {"00000000"}
password_confirmation {"00000000"}
end
end
⑤ Run the test
Terminal
bundle exec rspec spec/models/user_spec.rb
Recommended Posts