---
title: "You can use FactoryBot for non ActiveRecord models"
description: "You can use FactoryBot's initialize\\with* method to initialize an object any way you like which allows you to factorize any of your objects at will"
canonical: https://oozou.com/til/you-can-use-factorybot-for-non-activerecord-models-45
locale: en
date: 2020-10-13
topic: ruby
---

# You can use FactoryBot for non ActiveRecord models

> You can use FactoryBot's initialize\with* method to initialize an object any way you like which allows you to factorize any of your objects at will

Published: 2020-10-13 · Topic: [ruby](https://oozou.com/til-topics/ruby) ([markdown](https://oozou.com/til-topics/ruby.md))

You can use FactoryBot's *initialize\_with* method to initialize an object any way you like which allows you to factorize any of your objects at will

```ruby
FactoryBot.define do
  factory :citizen_id_image, class: CitizenIdImagePresenter do
    image_url { 'https://placehold.it/200x200' }

    initialize_with { new(image_url: image_url) }
  end
end
build(:citizen_id_image)
#=> 
```

# Introduction

FactoryBot is a popular testing tool for Ruby on Rails applications that allows developers to define and create test fixtures on the fly. It provides a flexible and efficient way to create test data, making it easier to write and maintain tests. In this article, we will explore how to use FactoryBot effectively in Rails testing.

# Setting up FactoryBot

To set up FactoryBot in your Rails application, you need to add the factory\_bot gem to your Gemfile and run the bundle install command. You can then configure FactoryBot by creating a factory\_bot.rb file in the config/initializers directory. In this file, you can define the factory\_bot configuration, such as the factory paths and the default strategy.

# Defining Models and Factories

In FactoryBot, a factory is a blueprint for creating an object. You can define a factory for a model by creating a file in the factories directory. For example, if you have a User model, you can create a user.rb file in the factories directory to define the user factory. In this file, you can define the attributes and associations for the user factory.

# Factory Options and Attributes

FactoryBot provides several options and attributes that you can use to customize your factories. For example, you can use the class attribute to specify the class of the object being created. You can also use the attributes option to define the attributes for the factory. Additionally, you can use the association option to define associations between factories.

# Conclusion

In conclusion, FactoryBot is a powerful tool for creating test fixtures in Rails applications. By defining factories for your models, you can create test data quickly and efficiently. With its flexible configuration options and customizable attributes, FactoryBot makes it easy to write and maintain tests. By following the best practices outlined in this article, you can get the most out of FactoryBot and improve the quality of your tests.

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
