Factory Bot (Rails Testing)

Last updated

Factory Bot, originally known as Factory Girl, [1] is a software library for the Ruby programming language that provides factory methods to create test fixtures for automated software testing. The fixture objects can be created on the fly; they may be plain Ruby objects with a predefined state, ORM objects with existing database records or mock objects.

Contents

Factory Bot is often used in testing Ruby on Rails applications; where it replaces Rails' built-in fixture mechanism. Rails' default setup uses a pre-populated database as test fixtures, which are global for the complete test suite. Factory Bot, on the other hand, allows developers to define a different setup for each test and thus helps to avoid dependencies within the test suite. [2] [3]

Factories

Defining Factories

A factory is defined by a name and its set of attributes. The class of the test object is either determined through the name of the factory or set explicitly. [4]

FactoryBot.definedo# Determine class automaticallyfactory:userdoname{"Captain Minion"}superhero{false}end# Specify class explicitlyfactory:superhero,class:Userdoname{"Tony Stark"}superhero{true}endend

Features

Traits

Traits allow grouping of attributes which can be applied to any factory. [4] [5]

factory:statusdotitle{"Seeking for Full Time jobs"}trait:internationaldointernational{true}endtrait:residentdointernational{false}endtrait:comp_scidocomp_sci{true}endtrait:electricaldocomp_sci{false}endfactory:comp_sci_international_student,traits:[:international,:comp_sci]factory:electrical_resident_student,traits:[:resident,:electrical]end

Alias

Factory Bot allows creating aliases for existing factories so that the factories can be reused. [4]

factory:user,aliases:[:student,:teacher]dofirst_name{"John"}endfactory:noticedoteacher# Alias used ''teacher'' for ''user''title{"Office Hours"}endfactory:notificationdostudent#Alias used student for user title{"Lecture timings"}end

Sequences

Factory Bot allows creating unique values for a test attribute in a given format. [4]

FactoryBot.definedofactory:titledosequence(:name){|n|"Title #{n}"}# Title 1, Title 2 and so on...endend

Inheritance

Factories can be inherited while creating a factory for a class. This allows the user to reuse common attributes from parent factories and avoid writing duplicate code for duplicate attributes. Factories can be written in a nested fashion to leverage inheritance.

factory:userdoname{"Micheal"}factory:admindoadmin_rightstrueendendadmin_user=create(:admin)admin_user.name# Michealadmin_user.admin_rights# true

Parent factories can also be specified explicitly.

factory:userdoname{"Micheal"}endfactory:admin,parent::userdoadmin_user{true}end

Callback

Factory Bot allows custom code to be injected at four different stages: [4]

after(:build)
Code can be injected after the factory is built
before(:create)
Code can be injected before the factory is saved
after(:create)
Code can be injected after the factory is saved
after(:stub)
Code can be injected before the factory is stubbed

See also

Other Test libraries for Ruby

References

  1. "Project Naming History". github.com/thoughtbot/factory_bot.
  2. "Waiting For a FactoryGirl". Thoughtbot Blog.
  3. "Rails Testing". hiringthing.com.
  4. 1 2 3 4 5 "Getting Started". rubydoc.info/gems/factory_bot.
  5. "Traits". Thoughtbot Blog.