ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina
[Ruby on Rails] Until RSpec is introduced We will proceed on the premise that this is done.
This time we will test if there is a title column and the number of characters.
app/models/post.rb
class Post < ApplicationRecord
belongs_to :user
validates :title, presence: true, length: {maximum: 20}
end
① Create models folder and factories folder under spec, Also create a file for the model you want to test. This time we will test the post model. Also, to ensure that users can only post while logged in, Also create a use model. The file structure is as follows. spec/models/post_spec.rb → Describe what you want to test
spec/factories/post.rb spec/factories/user.rb → Create dummy data
② Enables Factory Bot. It is convenient to use it because you can register the DB and build the model like user = create (: user). Create a support folder and factory_bot.rb file under spec and describe as follows.
spec/support/factory_bot.rb
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
Then add the following:
spec/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'support/factory_bot' #<-Addition
...
First, create dummy data.
spec/factories/user.rb
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
phone_number { 12345678909 }
password { 'password' }
password_confirmation { 'password' }
end
end
spec/factories/post.rb
FactoryBot.define do
factory :post do
body { Faker::Lorem.characters(number:20) }
user
end
end
Next, write the test code.
spec/models/post_spec.rb
require 'rails_helper'
RSpec.describe 'Post model testing', type: :model do
describe 'Validation test' do
#Use the dummy data created by factories.
let(:user) { FactoryBot.create(:user) }
let!(:post) { build(:post, user_id: user.id) }
# test_Create a post and check if you can register in the blank.
subject { test_post.valid? }
let(:test_post) { post }
context 'title column' do
it 'Must not be blank' do
test_post.title = ''
is_expected.to eq false;
end
it 'Must be 20 characters or less' do
post.title = Faker::Lorem.characters(number:21)
expect(post.valid?).to eq false;
end
end
end
describe 'Testing the association' do
context 'Relationship with customer model' do
it 'N:Is 1' do
expect(Post.reflect_on_association(:user).macro).to eq :belongs_to
end
end
# has_It is also described by many relationships.
# context 'Relationship with PostComment model' do
# it '1:Is N' do
# expect(Post.reflect_on_association(:post_comments).macro).to eq :has_many
# end
# end
end
end
Then do the following in your terminal:
$ rspec spec/models
If you pass the test
Finished in 0.52408 seconds (files took 2.11 seconds to load)
3 examples, 0 failures
Since it is displayed like this, it means that the test content is correct.
On the contrary, if it does not pass the test, you can see where the error is occurring in this way, so You will be able to see if the test code is wrong, the validation is wrong, and so on.
Failures:
1)Post model test Validation test title column Must be 20 characters or less
Failure/Error: let!(:post) { build(:post) }
NoMethodError:
undefined method `build' for #<RSpec::ExampleGroups::Post::Nested::Title:0x000000000619e938>
# ./spec/models/post_spec.rb:9:in `block (3 levels) in <top (required)>'
2)Post model test Validation test title column Must not be blank
Failure/Error: let!(:post) { build(:post) }
NoMethodError:
undefined method `build' for #<RSpec::ExampleGroups::Post::Nested::Title:0x0000000007491518>
# ./spec/models/post_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.07992 seconds (files took 2.41 seconds to load)
2 examples, 2 failures
Failed examples:
rspec ./spec/models/post_spec.rb:11 #Post model test Validation test title column Must be 20 characters or less
rspec ./spec/models/post_spec.rb:15 #Post model test Validation test title column Must not be blank
Also, using rspec ./spec/models/post_spec.rb:11 at the bottom, You can also check the test contents individually as shown below.
Terminal
$ rspec spec/models/post_spec.rb:11
This time 1, Validation to prevent registration with blank presence: true 2, Validation of character limit length: {maximum: 20} 3, Confirmation of relationship relationships belongs_to: user
I tested the above, but there are many other testing methods, so If you are interested, please check it out.
Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork
Recommended Posts