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.
2,641
questions
126
votes
18
answers
66k
views
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
...
125
votes
11
answers
48k
views
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 ...
120
votes
8
answers
36k
views
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
...
115
votes
3
answers
65k
views
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 ...
111
votes
4
answers
58k
views
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 > ...
103
votes
7
answers
95k
views
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 ...
...
99
votes
12
answers
47k
views
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 ...
87
votes
4
answers
18k
views
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....
86
votes
2
answers
39k
views
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 ...
82
votes
5
answers
45k
views
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 ...
77
votes
9
answers
32k
views
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 => :...
76
votes
4
answers
58k
views
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 ...
69
votes
6
answers
29k
views
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 ...
65
votes
3
answers
25k
views
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 ...
62
votes
9
answers
31k
views
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 ...
57
votes
5
answers
41k
views
How do I simulate a login with RSpec?
I have been playing with Rails for a couple of years now and have produced a couple of passable apps that are in production. I've always avoided doing any testing though and I have decided to rectify ...
52
votes
6
answers
66k
views
ActiveModel::MissingAttributeError: can't write unknown attribute `ad_id' with FactoryGirl
I have the following models:
class Ad < ActiveRecord::Base
belongs_to :page
has_one :image
has_one :logo
end
class Page < ActiveRecord::Base
has_many :logos
has_many :images
...
49
votes
28
answers
33k
views
Factory already registered: user (FactoryGirl::DuplicateDefinitionError)
Description of problem:
- I've setup factory_girl_rails however whenever I try and load a factory it's trying to load it multiple times.
Environment:
- rails (3.2.1)
- factory_girl (2.5.2)
- ...
47
votes
10
answers
23k
views
"undefined method `env' for nil:NilClass" in 'setup_controller_for_warden' error when testing Devise using Rspec
I'm trying to create a spec for a sign out flow by using factorygirl to create a user and then use Devise's sign_in method to authenticate the user, then use capybara to click the "Sign Out" link.
I'...
43
votes
1
answer
26k
views
Meaning for attributes_for in FactoryGirl and Rspec Testing
Looking through a tutorial on controller testing the author gives an example of an rspec test testing a controller action. My question is, why did they use the method attributes_for over build? There ...
42
votes
3
answers
19k
views
FactoryGirl build_stubbed strategy with a has_many association
Given a standard has_many relationship between two objects. For a simple example, let's go with:
class Order < ActiveRecord::Base
has_many :line_items
end
class LineItem < ActiveRecord::Base
...
42
votes
6
answers
36k
views
How to test model's callback method independently?
I had a method in a model:
class Article < ActiveRecord::Base
def do_something
end
end
I also had a unit test for this method:
# spec/models/article_spec.rb
describe "#do_something" do
@...
42
votes
10
answers
16k
views
Using factory_girl in Rails with associations that have unique constraints. Getting duplicate errors
I'm working with a Rails 2.2 project working to update it. I'm replacing existing fixtures with factories (using factory_girl) and have had some issues. The problem is with models that represent ...
42
votes
5
answers
19k
views
Populating an association with children in factory_girl
I have a model Foo that has_many 'Bar'. I have a factory_girl factory for each of these objects. The factory for Bar has an association to Foo; it will instantiate a Foo when it creates the Bar.
I'd ...
40
votes
4
answers
20k
views
Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory
Ive been following this introductory to Rails testing and Ive run into an issue I cant seem to find the solution to. Im very familiar with Rails but this is my first foray into testing.
Anyhow, I ...
40
votes
6
answers
17k
views
Upgrading to devise 3.1 => getting Reset password token is invalid
Solution
Thanks to this gist form Steven Harman, I got it working.
devise_mail_helpers.rb
module Features
module MailHelpers
def last_email
ActionMailer::Base.deliveries[0]
end
...
40
votes
3
answers
9k
views
Machinist vs FactoryGirl - pros and cons
I'm working with factory_girl, but looking at the machinist gem. Could you tell me please - what are the pros and cons of migrating to machinist? Have you compared those libs?
39
votes
4
answers
22k
views
Rails: Avoiding duplication errors in Factory Girl...am I doing it wrong?
Suppose I have a model user, which has a uniqueness constraint on the email field
If I call Factory(:user) once all is well, but if I call it a second time it'll fail with an "entry already exists" ...
39
votes
3
answers
45k
views
How to specify the association with factory_bot?
For example I have two models a user and a post. A post belongs_to a user and a user has many posts
#spec/factories/post.rb
FactoryBot.define do
factory :post do
user
body Faker::Movie.quote
...
38
votes
2
answers
12k
views
FactoryGirl + Faker - same data being generated for every object in db seed data
I am using FactoryGirl and Faker to generate user objects in my seeds.rb file but for some reason the exact same user is being created and rake db:seed is failing because of an email uniqueness ...
37
votes
2
answers
11k
views
Factory Girl + Mongoid embedded documents in fixtures
Let’s say you have the following mongoid documents:
class User
include Mongoid::Document
embeds_one :name
end
class UserName
include Mongoid::Document
field :first
field :...
36
votes
6
answers
21k
views
How can I use FactoryBot in db/seeds?
Is it possible to do this?
If so, how can you do it?
Note: FactoryBot was previously named FactoryGirl
36
votes
5
answers
12k
views
Using factory_girl_rails with Rspec on namespaced models
I have a web service that serves Ads to several different clients. The structure of the Ad varies between clients, and therefore, I am using namespaces for my models and controllers by the client name ...
36
votes
4
answers
44k
views
How to write an RSpec test for a simple PUT update?
I'm trying to solidify my understanding of rails and the BDD workflow, so I wanted to start small by creating one of those mini-blogs, but with rspec. Right now I have an ArticlesController and ...
35
votes
2
answers
13k
views
how to define factories with a inheritance user model
I got following problem:
In my application i use inheritance to define my user model:
class User
include Mongoid::Document
field :name...
field :bla...
end
class CustomUser < User
field :...
34
votes
2
answers
43k
views
FactoryBot to build list of objects with trait
I'm using factory_bot to create objects in my test, here is a example of my factory:
factory :user do
name "John"
surname "Doe"
trait :with_photo do
ignore do
photo_count 1
end
...
34
votes
5
answers
12k
views
Why isn't factory_girl operating transactionally for me? - rows remain in database after tests
I'm trying to use factory_girl to create a "user" factory (with RSpec) however it doesn't seem to be operating transactionally and is apparently failing because of remnant data from previous tests in ...
33
votes
2
answers
21k
views
Pass parameter in setting attribute on association in FactoryGirl
I want to pass a parameter into a factory that will be used to set the attribute on an associated model. The associated model is created within the factory.
I have a Transaction model with a many-to-...
33
votes
6
answers
20k
views
Factory Girl: How to set up a has_many/through association
I've been struggling with setting up a has_many/through relationship using Factory Girl.
I have the following models:
class Job < ActiveRecord::Base
has_many :job_details, :dependent => :...
32
votes
4
answers
9k
views
FactoryGirl screws up rake db:migrate process
I am doing TDD/BDD in Ruby on Rails 3 with Rspec (2.11.0) and FactoryGirl (4.0.0). I have a factory for a Category model:
FactoryGirl.define "Category" do
factory :category do
name "Foo"
end
...
32
votes
5
answers
26k
views
Ruby Gem: Uninitialized constant FactoryBot
Working on a Ruby gem and trying to use FactoryBot inside with RSpec.
I have this in support/factory_bot.rb:
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(...
32
votes
1
answer
12k
views
Testing simple STI with FactoryGirl
I have got a class, that is the base of some other classes that specializes the behavior:
class Task < ActiveRecord::Base
attr_accessible :type, :name, :command
validates_presence_of :type, :...
32
votes
1
answer
8k
views
Setting roles through rolify in FactoryBot definition
I am using devise, rolify and cancan. I'm also using RSpec with FactoryBot for testing. Right now I'm working on some tests and I want to define users with different roles for those tests. Here is my ...
32
votes
2
answers
10k
views
What's the difference between mock, stub, and factory girl?
I'm pretty new to rspec and the whole TDD methodology. Can someone please explain the difference between mock and stub. When do we use them and when do we use Factory Girl to create objects in test ...
31
votes
9
answers
15k
views
How can I reset a factory_girl sequence?
Provided that I have a project factory
Factory.define :project do |p|
p.sequence(:title) { |n| "project #{n} title" }
p.sequence(:subtitle) { |n| "project #{n} ...
31
votes
6
answers
16k
views
How to complete the rspec put controller test from scaffold
I'm using scaffolding to generate rspec controller tests. By default, it creates the test as:
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
describe "PUT ...
31
votes
9
answers
24k
views
"Could not find a valid mapping for #<User ...>" only on second and successive tests
I'm trying to write a request test that asserts that the proper links appear on the application layout depending in whether a user is logged in or out. FWIW, I'm using Devise for the authentication ...
31
votes
7
answers
12k
views
FactoryGirl: attributes_for not giving me associated attributes
I have a Code model factory like this:
Factory.define :code do |f|
f.value "code"
f.association :code_type
f.association(:codeable, :factory => :portfolio)
end
But when I test my ...
30
votes
6
answers
19k
views
Factory not registered: user
I am following Michal Hartls Rails tutorial Chapter 7. I have installed the Factory girl gem. I keep getting this error when I run my test
Failures:
1) UsersController GET 'show' should be ...
30
votes
3
answers
11k
views
RSpec failure: could not find table after migration...?
I have a naked rails 3 app with one model, generated using rails g model User.
I've added a factory (using factory_girl_rails):
Factory.define :user do |f|
f.email "[email protected]"
f.password "...