Should we use Faker in the Rails Factory?

I like Faker , I use it all the time in my seeds.rb to populate my application environment with real data.

I also started using Factory Girl , which also saves a lot of time - but when I go around the Internet for code examples. I do not see much evidence that people unite them.

Q. Is there a good reason why people do not use faker in the factory?

I feel that by doing this I would increase the reliability of my tests by exposing random but predictable data each time, which I hope will increase the chances of an error.

But perhaps this is not true, and there is no benefit from hard coding factory, or I do not see a potential error. Is there a good reason why these two stones should or should not be combined?

+6
source share
3 answers

Some people object to this, here .

DO NOT USE FREE ATTRIBUTES

One common pattern is to use a fake data library (like Faker or Forgery) to generate random values ​​per fly. This may seem appealing to names, email addresses, or phone numbers, but it does not have a real purpose. Creating unique values ​​is simple enough with sequences:

 FactoryGirl.define do sequence(:title) { |n| "Example title #{n}" } factory :post do title end end FactoryGirl.create(:post).title # => 'Example title 1' 

Your randomized data may at some point trigger unexpected results in your tests, which makes your factories disappointing to work with. Any value that may affect the test result in some way will have to be redefined, which means:

Over time, you will discover new attributes that sometimes cause. This disappoints the process, because tests can only once every ten or hundreds of runs - depending on how many attributes and possible values ​​are, and which combination causes the error. You will need to list each such random attribute in each test in order to override it, which is stupid. Thus, you create non-random factories, thereby denying any benefit from the original chance. It can be argued, as Henrik Sniff does, that random values ​​help you detect errors. While this is possible, it means you have a bigger problem: holes in your test suite. In the worst case, the error still goes unnoticed; at best, you get a cryptic error message that disappears the next time you run the test, it's hard to debug. True, a critical error is better than an error, but randomized factories remain a poor substitute for proper unit testing, code, and TDD to prevent these problems.

Therefore, randomized factories are not only not worth the effort, they even give you false confidence in their tests, which is worse than not having any tests at all.

But you have nothing to do, if you want, just do it.

Oh, and there’s an even easier way to embed a sequence in a recent FactoryGirl, this quote was written for an older version.

+5
source

I like to use Faker and usually do this when working with large code bases. I see the following advantages and disadvantages when using Faker with Factory Girl:

Possible disadvantages:

  • It's a little harder to reproduce the same test case (at least RSpec works around this, each time displaying the seed of a random number generator and letting you play the same thing with it)
  • Data generation leads to reduced performance.

Possible advantages:

  • Makes the displayed data more understandable to humans. When creating test data manually, people are prone to all short cuts to avoid fatigue.
  • Building factories with Faker for testing at the same time provides you with the means to create good demo data for presentations.
  • You may randomly detect edge errors during repeated testing.
+1
source

It is for you.

In my opinion, it is a very good idea to have random data in the tests, and this always helped me detect errors and corner cases that I did not think about.

I never regret random data. All points described by @jrochkind would be correct (and you should read a different answer before reading this), but it is also true that you can (and should) write this in your spec_helper.rb

 config.before(:all) { Faker::Config.random = Random.new(config.seed) } 

this will make you have duplicate tests with duplicate data. If you do not, then you have all the problems described in another answer.

+1
source

All Articles