I want to randomly generate information when writing test code

【Overview】

1. Conclusion </ b>

2. Installation method </ b>

3. How to write a nickname, email, password </ b>

4. Another way to write </ b>

Supplement: How to write date / last name / first name (kanji and katakana) </ b>


1. Conclusion ---------------------------------------- Put gem "faker" in gemfile and bundle install. Then, create a facroies folder in the spec folder, create the file you want to describe, and program it.
  1. Installation method

  • I will omit the introduction of gem "rspec-rails" and gem "factory_bot_rails" here! I will talk about the two on the assumption that you have bundle install. If you posted an article about "Rspec", "FactoryBot", we'll post a link here as well!
    Step ❶

gemfile folder


group :development, :test do
end 

In

gem 'faker'

Described.


Step ❷

Terminal


%bundle install 

Write like this in the terminal.


Procedure ❸ Create a factories folder in the spec folder Create any file. (For Ruby, "model name.rb")
Procedure ❹

factories/***.rb


FactoryBot.define do
  factory :user do
-------I will describe it below here------------


 end
end

  1. How to write a nickname and email

”-----I will describe it below here--------”

In the column of

factories/***.rb


nickname { Faker::Name.name }
email { Faker::Internet.free_email }

You can generate it randomly by writing! nickname and email have the same name as the DB column.


3. How to write a password

factories/***.rb


password = Faker::Internet.password(min_length: 6)
password { password }
password_confirmation { password }

min_length: 6 matches the inherent validation of gem "devise". It also includes a "confirmation password" to enter twice.

  1. How to check if it is generated

At the terminal

Terminal


%rails c

And after opening the console

console


pry(main)>FactoryBot.create(:Model name)
  • pry is because we have introduced binding.pry.

If you enter the following, you can see that the nickname and email information are randomly generated!

  • The password is encrypted with encrypted_password.

console


nickname: "Sen. Lou Schimmel", email: "[email protected]"


4. Another way of writing

If it is a pattern that does not introduce gem "faker", You can also write like this!

factories/***.rb


FactoryBot.define do
  factory :user do

  nickname              {"taro"}
  email                 {"[email protected]"}
end
end


Supplement. How to write date / last name / first name (kanji and katakana)

From the conclusion,

factories/***.rb


FactoryBot.define do
  factory :user do
    transient do
      person { Gimei.name }
   end
   date { Faker::Date.backward }
   first_name { person.first.kanji }
   last_name { person.last.kanji }
   first_name_kana { person.first.katakana }
   last_name_kana { person.last.katakana }
 end
end

Write like this. However, there is one thing I would like you to be aware of. Since kanji and katakana (phonetic kana) cannot be randomly generated in gem "faker", separately write gem "gimei" in the gemfile and perform bandle install.

And

factories/***.rb


transient do
person { Gimei.name }

This gives you the freedom to use the gem.

When actually using Put "person." It can be specified by last name and first name (“first” / ”last”), It can be specified with kanji and katakana ("kanji" / "katakana").

Recommended Posts