Password dummy data generation notes in Rails model unit test code

Article posting background

Faker was used to create dummy data </ font> in the model unit test. Faker randomly generates names, emails, etc. However, I found that be careful when generating password data </ font> (see below). Therefore, post an article as a memorandum with Notes and remedies </ font>.

** Supplement ** Faker is one of the Ruby on Rails gems. Values can be randomly generated by using faker. Please refer to the link below for details.

Faker official Github

Precautions for password generation using Faker

Dummy data to the password column was generated by the following description.

user.rb


FactoryBot.define do
  factory :user do
    nickname                { Faker::Name.name }
    email                   { Faker::Internet.free_email }
    password                { Faker::Internet.password(min_length: 8) }
    password_confirmation   { password }
  end
end

The password generated at this time is usually a combination of numbers and strings, but rarely only strings or numbers are generated </ font>. This means that if password validation is a combination of numbers and strings </ font>, it will be unsuitable as dummy data </ font>. ..

Terminal


[8] pry(#<RSpec::ExampleGroups::User::Create>)> @user = FactoryBot.build(:user)
=> #<User id: nil, email: "[email protected]", nickname: "Eugenie Dach", created_at: nil, updated_at: nil>
[9] pry(#<RSpec::ExampleGroups::User::Create>)> @user.password
=> "1HyWz2Mr"
[10] pry(#<RSpec::ExampleGroups::User::Create>)> @user = FactoryBot.build(:user)
=> #<User id: nil, email: "[email protected]", nickname: "Kathi D'Amore", created_at: nil, updated_at: nil>
[11] pry(#<RSpec::ExampleGroups::User::Create>)> @user.password
=> "NdKrAcLw"

Workaround

If you have validated the combination of numbers and character strings for the password, manually create dummy data for the password </ font>.

user.rb


FactoryBot.define do
  factory :user do
    nickname                { Faker::Name.name }
    email                   { Faker::Internet.free_email }
    password                { 'test1234TEST' }
    password_confirmation   { password }
  end
end

that's all