For the flow up to the introduction of RSpec, click here [https://qiita.com/TerToEer_sho/items/472e14df6fbb8e83ebf9)
Prepare directories and files. ex) For user model FactoryBot, spec / factories / users.rb
spec/factories/users.rb
FactoryBot.define do
factory :user do
email {Faker::Internet.free_email} #Example
#Below, describe the necessary Faker in the same way
end
end
The: user part is used when calling Faker in spec / models / user_spec.rb.
For details on how to use Faker, go to Faker's GitHub
At the terminal
rails g rspec:model model name
With this command spec / models / model name_spec.rb File is generated.
From the beginning in the file
spec/models/user_spec.rb
RSpec.describe User, type: :model do
pending "add some examples to (or delete) #{__FILE__}" #Delete this line
end
It contains code like this. You can delete the code on the second line. (In the above example, user is specified as the model name)
spec/models/user_spec.rb
RSpec.describe User, type: :model do
describe 'What to test (Example) New user registration' do
before do
@user = FactoryBot.build(:user) #Taking the user model as an example, call user's FactoryBot
end
it "Specific test items (example) Email address is required" do
end
end
end
(Similarly, in the above example, user is specified as the model name.)
.build (: user) ←: user is called from FactoryBot.
Put an example between ʻit and
do. Write the code between
do ~ end`.
Recommended Posts