factory-bot

factory girl nested factory

99封情书 提交于 2019-11-28 20:45:31
I have an account model that belongs_to a role model. factory :role do name "student" end factory :account do user role end The first factory creates a role named "student". The second factory creates an account that is associated to the student role that was created in the previous factory. It also is associated with a user...which is not important for this question. I have many roles to be tested (admin, student, assistant)... I dont want to specify 'student' in the role factory...thats too static. How do I specify what role to create at the time the account factory is created? Like: factory

ArgumentError: Factory not registered

邮差的信 提交于 2019-11-28 20:24:53
I am trying to get factory girl to run with rspec in my rails 4.1.1 app. Problem is when I run rspec in my command line, i get Failure/Error: verse = build(:verse) ArgumentError: Factory not registered: verse . I am at loss because I checked the factory girl getting started page and many answers here on SO andI still can't fix this issue. in my Gemfile: gem 'rails', '4.1.1' group :development, :test do gem 'rspec-rails' gem "factory_girl_rails" end my spec_helper.rb file: require 'factory_girl_rails' RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end spec/controllers

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

不打扰是莪最后的温柔 提交于 2019-11-28 16:55:18
问题 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 = FactoryGirl.create(:user) I still doesn't understand the practical differences between the two. Can someone give an example where you would want to use one and not the other? Thanks! 回答1: The create() method persists the instance of the model while the build() method

FactoryGirl and polymorphic associations

…衆ロ難τιáo~ 提交于 2019-11-28 16:24:41
问题 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 < ActiveRecord::Base belongs_to :profile, :dependent => :destroy, :polymorphic => true end class Artist < ActiveRecord::Base has_one :user, :as => :profile end class Musician < ActiveRecord::Base has_one :user, :as => :profile end After choosing this

Populating an association with children in factory_girl

半世苍凉 提交于 2019-11-28 16:05:24
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 like a Factory that creates a Foo that contains a Bar. Ideally this Bar would be created through the :bar factory, and respect the build strategy (create/build) used to create the Foo. I know I could just call the :bar factory and then grab the Foo reference from the new Bar. I'd like to avoid this; in my test case, the important object is Foo; calling the Bar factory seems a bit circuitous. Also,

Find or create record through factory_girl association

ε祈祈猫儿з 提交于 2019-11-28 15:55:13
问题 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| f.name "default" end When the first user is created a new group is created too. When I try to create a second user it fails because it wants to create same group again. Is there a way to tell factory_girl association method to look first for an existing

FactoryGirl build_stubbed strategy with a has_many association

岁酱吖の 提交于 2019-11-28 15:48:30
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 belongs_to :order end What I'd like to do is generate a stubbed order with a list of stubbed line items. FactoryGirl.define do factory :line_item do name 'An Item' quantity 1 end end FactoryGirl.define do factory :order do ignore do line_items_count 1 end after(:stub) do |order, evaluator| order.line_items = build_stubbed_list(:line_item, evaluator.line_items_count, :order => order) end end end The above

FactoryGirl has_many association with validation

你。 提交于 2019-11-28 07:33:04
I have a standard has_many relationship (Booking has many Orders) with validation that a Booking does not get saved without at least one Order. I'm trying to replicate this with my FactoryGirl factories but the validation is preventing me from doing so. class Booking < ActiveRecord::Base has_many :orders validates :orders, presence: true end class Order < ActiveRecord::Base belongs_to :booking end Here are my FactoyGirl factory specifications for each model as followed from FactoryGirl's GitHub wiki page. FactoryGirl.define do factory :booking do factory :booking_with_orders do ignore do

How to write an RSpec test for a simple PUT update?

那年仲夏 提交于 2019-11-28 06:46:29
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 Article model, and associated rspec files. Article is very simple, has just title:string and content:text, and the ArticlesController is RESTful - although I hand wrote the MCV for Article, it's basically the same as if I used a scaffold to create it. However I don't really know what I'm doing when it comes to writing a test in rspec for the PUT update. I'm using Factory Girl to create the article

How do I simulate a login with RSpec?

我只是一个虾纸丫 提交于 2019-11-28 04:28:33
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 that. I'm trying to write some tests for an app that I wrote for work that is already up and running but undergoing constant revision. I'm concerned that any changes will break things so I want to get some tests up and running. I've read the RSpec book, watched a few screencasts but am struggling to get started (it strikes me as the sort of thing you only understand once you've actually done it).