Factory Bot (Rails Testing)
Factory Bot, originally known as Factory Girl, is a software library for the Ruby programming language that provides factory methods to create test fixtures for automated software testing.
≈ 2 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Factory Bot, originally known as Factory Girl, 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.
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.
FactoryBot.define do # Determine class automatically factory :user do name { "Captain Minion" } superhero { false } end # Specify class explicitly factory :superhero, class: User do name { "Tony Stark" } superhero { true } end end factory :status do title { "Seeking for Full Time jobs" } trait :international do international { true } end trait :resident do international { false } end trait :comp_sci do comp_sci { true } end trait :electrical do comp_sci { false } end factory :comp_sci_international_student, traits: [:international, :comp_sci] factory :electrical_resident_student, traits: [:resident, :electrical] end factory :user, aliases: [:student, :teacher] do first_name { "John" } end factory :notice do teacher # Alias used teacher for user title { "Office Hours" } end factory :notification do student #Alias used student for user title { "Lecture timings" } end FactoryBot.define do factory :title do sequence(:name) {|n| "Title #{n}" } # Title 1, Title 2 and so on... end end factory :user do name { "Micheal" } 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 { "Micheal" } end factory :admin, parent: :user do admin_user { true } end
Text derived from https://en.wikipedia.org/w/index.php?oldid=1338364037 under CC BY-SA 4.0; modifications have been made.