Why use Factory Bot instead of creating test data manually?

04-Jun-2020 1223
I recently taught a Rails testing class where we wrote tests using RSpec, Capybara, Factory Bot and Faker.During the class, one of the students asked, why do we use Factory Bot? What’s the advantage over creating test data manually?The answer to this is perhaps most easily explained with an example. I’m going to show an example of a test setup that creates data manually, then a test setup that uses Factory Bot, so you can see the difference.In both examples, the test setup is for a test that needs two Customer records to exist. A Customer object has several attributes and a couple associations.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle