In my opinion, Faker and randomly generated data is usually an anti-pattern in tests.
First of all, with Faker tests become nondeterministic: run - crashed, run again - passed. Such flaky tests are extremely difficult to debug and annoying on CI.
Secondly, random data slows down tests. Especially in factories: I worked on a project where switching from Faker to sequence { ... }
accelerated the test suite by 12%.
Thirdly, Faker overuse leads to code duplication in the tests. For example, take a look at the test method that returns user's initials:
it "returns initials"
user = described_class.new(name: Faker::Name.name)
expected = user.name
.upcase
.split(" ")
.map { |part| part[0] }
.join("")
expect(user.initials).to eq(expected)
end
This is an incomprehensible and confusing test: if the #initials
method changes, the changes will have to be duplicated in the test as well. A better way would be to write it like this:
it "returns initials"
user = described_class.new(name: "Daniel Craig Jones")
expect(user.initials).to eq("DCJ")
end
I suggest you to take a closer look at Faker in your test suite: are you sure you need it?
Top comments (2)
Does this apply to PHP as well?
Yes, random data in the tests make them unrepeatable and flaky.