Danny Santos
Danny Santos

Reputation: 1160

Faker gem generating 2-3 letter strings

The Faker gem generates short, nonsense strings instead of what is described. For example, Faker::Job.title generates "et". If I have a feature test that expects not to find a Faker-generated string on the page, chances are it's going to fail if the string is "et". Surely this is unexpected behaviour, as nobody in the world has the job title "et".

This is my code, the most recent time I checked it the title was as expected, but the role and category were not:

# frozen_string_literal: true

shared_context 'with signatory attributes' do
  let(:first_name)   { Faker::Name.first_name }
  let(:last_name)    { Faker::Name.last_name }
  let(:email)        { Faker::Internet.email }
  let(:title)        { Faker::Job.title }
  let(:mobile)       { Faker::Number.number(10) }
  let(:employee_num) { Faker::Number.number(10) }
  let(:role)         { Faker::Job.title }
  let(:category)     { Faker::Job.title }
end

Upvotes: 1

Views: 917

Answers (3)

moveson
moveson

Reputation: 5213

Looks like Faker isn’t set up to make realistic job titles. But it’s easy to make your own random job titles. I would just sample your own custom array, like this:

let(:title) { %w[Admin Manager Engineer].sample }

Upvotes: 1

marcoPagni
marcoPagni

Reputation: 37

It seems like it pulls strings from its Lorem Ipsum String Set for some reason. Do you mind sharing your code?

Upvotes: 0

DonPaulie
DonPaulie

Reputation: 2214

You can use regex matcher with word boundaries instead of the short string only, but it is still not bullet-proof.

let(:first_name)   { /\b#{Faker::Name.first_name}\b/ }

But maybe it is better to stub the attribute on model itself and raise an Error if it is called.

Upvotes: 0

Related Questions