# Testing Rails Apps with FactoryBot and MiniTest

DevFeed: [Testing Rails Apps with FactoryBot and MiniTest](<https://devfeed.tech/articles/testing-rails-apps-with-factorybot-and-minitest-28292.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2022/07/27/testing-rails-apps-with-factorybot-and-minitest.html>)

Author: Fuzzygroup

Published: 2022-07-27T10:58:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Testing](<https://devfeed.tech/topics/testing.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [Code](<https://devfeed.tech/topics/code.md>), [Framework](<https://devfeed.tech/topics/framework.md>), [RSpec](<https://devfeed.tech/topics/rspec.md>)

Tags: [factorybot](<https://devfeed.tech/tags/factorybot.md>), [rails](<https://devfeed.tech/tags/rails.md>), [rspec](<https://devfeed.tech/tags/rspec.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

A tutorial on testing Rails applications with FactoryBot and MiniTest. It explains how to define factories, use traits for different scenarios such as pools and hot tubs, and test Rails model methods. The article notes that RSpec is not required for FactoryBot.

## Source excerpt

Pizza courtesy of Pizza for Ukraine! Donate Now to Pizza for Ukraine This blog post looks at testing rails apps with FactoryBot and MiniTest - the default test framework which ships with Rails. It does not use RSpec and RSpec is NOT required for use with FactoryBot. Note: I believe that MiniTest is the name of the standard rails testing framework. Oddly I've had issues confirming that so if I'm wrong please feel free to tell me. Creating a Factory A factory is a pluralized file just as is a fixture. Factories live, generally, in the test/factories/ directory. Let's say that you were modeling swimming pools. You might have this factory: FactoryBot.define do factory :pool do user water_chemistry_type name { "Swimming Pool" } pool_type { "pool"} length {38} width {18} units { "feet"} volume_units {"gallons"} shallow_end_depth { 3} deep_end_depth {7.67} end end What the above code says: Define a factory named :pool Reference two other models - user and water_chemistry_type Have a bunch of attributes that define the pool Creating Two Specific Factories The power of something like FactoryBot comes, however, not when we have a single instance of anything but when we have multiple instances that we can use to test different scenarios. Let's say that I have both a pool and a hot tub. Those have commonalities but also differences. We can model those difference as traits. FactoryBot.define do factory :pool do user water_chemistry_type trait :swimming_pool do name { "Swimming Pool" } pool_type { "pool"} length {38} width {18} units { "feet"} volume_units {"gallons"} shallow_end_depth { 3} deep_end_depth {7.67} end trait :hot_tub do name {"Hot Tub"} pool_type {"hot_tub"} length { 6 } width { 6 } units { "feet"} volume_units { "gallons" } depth { 3 } end end end What this does is: Create a pool factory With two common attributes - user and water_chemistry_type Add a trait named "swimming" pool which defines the attributes for a swimming pool. Add a trait named "hot_tub" which def