Factory Girl (Rails Testing)
   HOME

TheInfoList



OR:

Factory Bot, originally known as Factory Girl, is a software library for the
Ruby programming language Ruby is a general-purpose programming language. It was designed with an emphasis on programming productivity and simplicity. In Ruby, everything is an object, including primitive data types. It was developed in the mid-1990s by Yukihiro "Mat ...
that provides factory methods to create
test fixture A test fixture is a device used to consistently test some item, device, or piece of software. Test fixtures are used in the testing of electronics, software and physical devices. Electronics In testing electronic equipment such as circuit boa ...
s for automated
software testing Software testing is the act of checking whether software satisfies expectations. Software testing can provide objective, independent information about the Quality (business), quality of software and the risk of its failure to a User (computin ...
. 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 object In computer science, a mock object is an object that imitates a production object in limited ways. A programmer might use a mock object as a test double for software testing. A mock object can also be used in generic programming. Analogy A mo ...
s. Factory Bot is often used in testing
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pa ...
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.


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. FactoryBot.define do # Determine class automatically factory :user do name superhero end # Specify class explicitly factory :superhero, class: User do name superhero end end


Features


Traits

Traits allow grouping of attributes which can be applied to any factory. factory :status do title trait :international do international end trait :resident do international end trait :comp_sci do comp_sci end trait :electrical do comp_sci end factory :comp_sci_international_student, traits: international, :comp_sci factory :electrical_resident_student, traits: resident, :electricalend


Alias

Factory Bot allows creating aliases for existing factories so that the factories can be reused. factory :user, aliases: student, :teacherdo first_name end factory :notice do teacher # Alias used ''teacher'' for ''user'' title end factory :notification do student #Alias used student for user title end


Sequences

Factory Bot allows creating unique values for a test attribute in a given format. FactoryBot.define do factory :title do sequence(:name) # Title 1, Title 2 and so on... end end


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 :user do name factory :admin do admin_rights true end end admin_user = create(:admin) admin_user.name # Micheal admin_user.admin_rights # true Parent factories can also be specified explicitly. factory :user do name end factory :admin, parent: :user do admin_user end


Callback

Factory Bot allows custom code to be injected at four different stages: ;: Code can be injected after the factory is built ;: Code can be injected before the factory is saved ;: Code can be injected after the factory is saved ;: Code can be injected before the factory is stubbed


See also


Other Test libraries for Ruby

*'
NullDB
'': a way to speed up testing by avoiding database use. *'
Fixture Builder
'': a tool that compiles Ruby factories into fixtures before a test run. *'

'': an extension to test/unit with additional helpers, macros, and assertions. *
Rspec RSpec is a computer domain-specific language (DSL) (particular application domain) testing tool written in the programming language Ruby to test Ruby code. It is a behavior-driven development (BDD) framework which is extensively used in product ...
: a behavior-driven development framework *'
Capybara
'': Acceptance test framework for web applications.


References

{{Reflist Ruby (programming language)