OOZOU
與我們聯繫
Back to TIL
ruby

You can use FactoryBot for non ActiveRecord models

October 13, 2020

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

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.

More on ruby

rubyApril 30, 2021

Allow pasting multiline ruby blocks in Pry when using leading dot notation

We use leading dot notation a lot now e.g.

rubyOctober 14, 2020

Heredoc without interpolation

Sometimes you want to use Ruby's Heredoc without interpolation ('#{....}'). You can do this by adding single dashes around your keyword:

rubyOctober 13, 2020

Automagical Intermediate-level Hashtables in Ruby

All intermediate-level hashtables will be created automagically

有專案構想嗎?

我們很樂意聆聽您正在打造的產品。

開始對話hello@oozou.com
OOZOU Logo

曼谷 · 新加坡 · 香港

X
Facebook
Instagram
LinkedIn

服務

  • Web 開發
  • AI 與生成式 AI 解決方案
  • 行動應用程式開發
  • 資料分析與工程
  • UI/UX 設計與產品設計
  • 數位轉型

公司

  • 關於我們
  • 職涯
  • 部落格
  • 白皮書
  • 案例研究
  • Today I Learned
  • 聯繫我們

更多

  • 產業
  • 索取報價
  • 合作夥伴網絡
© 2026 OOZOU. 版權所有。
隱私政策行為準則反賄賂反貪腐政策