ActiveRecord (AR) is the main driver of incidental (read: superfluous) complexity in your app. Do not feed the beast.

Installing a library like factorybot1 feeds the beast. It seems to give you superpowers and ... bam bam bam you are creating objects for your tests like it is nobody's business. Well -- and, you have just hurt your future you. It is an amazing library that you shouldn't use. I believe that factorybot adoption has a net negative effect in large code bases.

Stop using factory_bot. Make your own ObjectMother

Using factory_bot makes the wrong thing easy: It makes tests easy that need to work with a complex AR object graph. You should keep that graph as simple as possible, not make it easier to grow and let it get out of control. Writing your own ObjectMother (a factory class that can generate the objects you need for your tests)2 doesn't immediately stop the problem of proliferating AR, but it points you in the right direction to reduce complexity in your app. I will show you how to try this out in any codebase - in three steps.

#1. Start an ObjectMother class

Change just one class to use your own ObjectMother and prove that it's possible. Check out the class in3 as a starting point.

#2. Grow the complexity of your generators only as much as you need

The code in3 can create AR classes, build them, generate params for them and it has default overrides (with lazy loading). Creating this and using this doesn't even take more lines of code than using factory_bot.

#3. Let implementation pain be your guide to simplifying

If you ever feel like writing the methods to create a new object is becoming painful, explore other patterns. Explore what you can simplify where (in your AR graph maybe?) to make building data for your tests less painful and refactor in that direction.

testing (4) test factories (1)


Mention this post from your site:

Except where otherwise noted, content on is licensed under CC BY 4.0 by Stephan Hagemann