Questions tagged [factory-bot]

factory_bot is a Ruby gem that allows you to quickly define prototypes for each of your models and ask for instances with properties that are important to the test at hand.

factory_bot (formerly factory_girl) is a Ruby gem which is replacement for Rails fixtures. It is used to construct test data. It supports multiple build strategies (create, build, attribute hashes) and associations.

If you want to use factory_bot with Rails, use the factory_bot_rails gem.


Resources

2638 questions
125
votes
18 answers

Skip callbacks on Factory Girl and Rspec

I'm testing a model with an after create callback that I'd like to run only on some occasions while testing. How can I skip/run callbacks from a factory? class User < ActiveRecord::Base after_create :run_something …
luizbranco
  • 2,841
  • 4
  • 18
  • 22
125
votes
11 answers

How to create has_and_belongs_to_many associations in Factory girl

Given the following class User < ActiveRecord::Base has_and_belongs_to_many :companies end class Company < ActiveRecord::Base has_and_belongs_to_many :users end how do you define factories for companies and users including the bidirectional…
opsb
  • 29,325
  • 19
  • 89
  • 99
120
votes
8 answers

How Do I Use Factory Girl To Generate A Paperclip Attachment?

I have model Person that has many Images, where images has a Paperclip attachment field called data, an abbreviated version displayed below: class Person has_many :images ... end class Image has_attached_file :data belongs_to :person …
Laz
  • 3,474
  • 9
  • 33
  • 46
112
votes
3 answers

What's the difference between the build and create methods in FactoryGirl?

The Factory Girl introduction delineates the difference between FactoryGirl.build() and FactoryGirl.create(): # Returns a User instance that's not saved user = FactoryGirl.build(:user) # Returns a saved User instance user =…
Son of the Wai-Pan
  • 12,371
  • 16
  • 46
  • 55
107
votes
4 answers

How do I use factories from FactoryBot in rails console

I am using rails console in the development environment and I want to use factories. How can I get access to them? I have tried require "FactoryBot" which returns 1.9.3p393 :301 > require "FactoryBot" LoadError: cannot load such file -- FactoryBot
Eric Baldwin
  • 3,341
  • 10
  • 31
  • 71
102
votes
7 answers

How to set up factory in FactoryBot with has_many association

Can someone tell me if I'm just going about the setup the wrong way? I have the following models that have has_many.through associations: class Listing < ActiveRecord::Base attr_accessible ... has_many :listing_features has_many :features,…
Tonys
  • 3,349
  • 4
  • 24
  • 27
97
votes
12 answers

Factory-girl create that bypasses my model validation

I am using Factory Girl to create two instances in my model/unit test for a Group. I am testing the model to check that a call to .current returns only the 'current' groups according to the expiry attribute as per below... describe ".current" do …
Norto23
  • 2,249
  • 3
  • 23
  • 40
87
votes
4 answers

Faker is producing duplicate data when used in factory_girl

I'm trying to populate some fake data into a factory using the Faker gem: Factory.define :user do |user| user.first_name Faker::Name::first_name user.last_name Faker::Name::last_name user.sequence(:email) {|n| "user#{n}@blow.com"…
Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
83
votes
2 answers

What is the purpose of a `transient do` block in FactoryBot factories?

What is the purpose of transient do in FactoryBot factories? I've seen a lot of factories that begin with something like below. factory :car do owner nil other_attribute nil end ... I've found some information on this blog: Using FactoryGirl to…
tim_xyz
  • 11,573
  • 17
  • 52
  • 97
82
votes
5 answers

How to define an array / hash in factory_bot?

I am trying to write a test that simulates some return values from Dropbox's REST service that gives me back data in an Array, with a nested hash. I am having trouble figuring out how to code my Factory since the return result is an array with a has…
Doug
  • 14,387
  • 17
  • 74
  • 104
76
votes
4 answers

FactoryBot: create the same object multiple times

In one of my RSpec test, I am creating multiple objects from the same factory definition Eg FactoryBot.create(:model_1) FactoryBot.create(:model_1) FactoryBot.create(:model_1) Is there a method that factory_bot provides to do this in one line I…
usha
  • 28,973
  • 5
  • 72
  • 93
75
votes
9 answers

Find or create record through factory_girl association

I have a User model that belongs to a Group. Group must have unique name attribute. User factory and group factory are defined as: Factory.define :user do |f| f.association :group, :factory => :group # ... end Factory.define :group do |f| …
Slobodan Kovacevic
  • 6,848
  • 3
  • 29
  • 33
69
votes
6 answers

FactoryGirl and polymorphic associations

The design I have a User model that belongs to a profile through a polymorphic association. The reason I chose this design can be found here. To summarize, there are many users of the application that have really different profiles. class User <…
Feech
  • 4,072
  • 4
  • 28
  • 36
65
votes
3 answers

Factory Girl - what's the purpose?

What's the purpose of Factory Girl in rspec tests when I could use before(:each) blocks? It feels like the only difference between Factory Girl and a before(:each) is that the factory prepares object creation outside of the test. Is this right?
curiousdork
  • 1,034
  • 2
  • 12
  • 13
62
votes
9 answers

Can FactoryBot generate factories after your models have been created?

When including the factory_bot_rails gem in your dev and test blocks in Gemfile, rails will generate factories automatically when your models are generated. Is there a way to generate factories after your models have been generated? Note:…
GuiDoody
  • 897
  • 1
  • 6
  • 13
1
2 3
99 100