Be careful not to duplicate web-console
Gemfile
gem 'rspec-rails'
group :development do
gem 'web-console'
end
After bundle install`` rails g rspec: install
Edit the .rspec file to make it easier to read:
.rspec
--format documentation
At this point, let's check if RSpec works properly.
bundle exec rspec
Terminal
If it works normally, it will be like this
No examples found.
Finished in 0.00031 seconds (files took 0.19956 seconds to load)
0 examples, 0 failures
.rspec spec spec/spec_helper.rb Like rails_helper.rb, it is a file that writes common settings for RSpec, but this is used when using RSpec without Rails.
spec/rails_helper.rb It is a file to write common settings when using RSpec in Rails. Apply common settings and methods by reading this file in each test file
Gemfile
group :development, :test do
#abridgement
gem 'rspec-rails'
gem 'factory_bot_rails'
end
** Creating a file ** Create a Ruby file with the plural file name of the created instance Created this time so that it becomes spec / factories / ʻusers.eb`.
** Edit file ** Make it easy to instantiate or save to DB by a specific method in the spec file
user.rb
FactoryBot.define do
factory :user do
nickname {"abe"}
email {"[email protected]"}
password {"00000000"}
password_confirmation {"00000000"}
end
end
** build method ** Create an instance of the class name taken as a symbol type as an argument based on the description of factory_bot
Example
#factory_When not using a bot
user = User.new(nickname: "abe", email: "[email protected]", password: "00000000", password_confirmation: "00000000")
#factory_When using a bot
user = FactoryBot.build(:user)
** create method ** It works almost the same as build, but in the case of create, the value is saved in the test DB. The test is executed once, and the contents of the test DB are rolled back each time it is completed.
Example
#The created instance is saved in the DB
user = FactoryBot.create(:user)
When creating an instance with factory_bot, you can omit the description of FactoryBot of the class that is the receiver.
rails_helper.rb
#abridgement
RSpec.configure do |config|
#Added the following description
config.include FactoryBot::Syntax::Methods
#abridgement
end
ʻUser modeltest code ** Create spec file ** File naming convention
corresponding class name_spec.rb`
Create so that it becomes spec / models / user_spec.rb.
user_spec.rb
require 'rails_helper'
describe User do
describe '#create' do
it "Cannot register without nickname" do
user = build(:user, nickname: "")
user.valid?
expect(user.errors[:nickname]).to include("can't be blank")
end
end
end
describe describe creates a set of tests immediately after do ~ end. In the "" that follows the describe, write a description of the group.
** it and example ** it represents a set of working test code called an example. Write a description of the example in the "" that follows it.
** Expectation ** It is the formula that is actually evaluated. Write between it do ~ end. In the above formula, ʻexpect (user.errors [: nickname]). To include ("can't be blank") `is the expectation.
** Matcha ** Shows the conditions under which the test will succeed in the expectation.
valid? Check "Is it in a state where it cannot be saved due to validation?" The return value of the method is true / false
errors If you use the errors method for an instance that uses the valid? method, check why it cannot be saved if it cannot be saved due to validation.
include You can check if the value taken as an argument is included in the array that is the argument of expect Duplicate pattern: has already been taken Empty pattern: can't be blank Insufficient character pattern: is too short Pattern with too many characters: is too long
be_valid It passes when the instance set as the argument of expect clears all validations. Example below
sample_spec.rb
it "Can be registered if nickname is 6 characters or less" do
user = build(:user, nickname: "aaaaaa")
expect(user).to be_valid
end
eq If they are equal, they will pass. Example below
sample_spec.rb
describe "sum" do
it "1 +1 becomes 2" do
expect(1 + 1).to eq 2
end
end
Recommended Posts