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>
gemfile folder
group :development, :test do
end
In
gem 'faker'
Described.
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
”-----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.
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.
At the terminal
Terminal
%rails c
And after opening the console
console
pry(main)>FactoryBot.create(:Model name)
If you enter the following, you can see that the nickname and email information are randomly generated!
console
nickname: "Sen. Lou Schimmel", email: "[email protected]"
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
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