factory-bot

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

*爱你&永不变心* 提交于 2019-12-21 06:46:30
问题 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: http://blog.thefrontiergroup.com.au/2014/12/using-factorygirl-easily-create-complex-data-sets-rails/ But I still don't fully understand how and why to do this. My experience with FactoryBot is minimal. Could anyone with some experience using FactoryBot share some insight?

factory girl multiple has_many through's

一世执手 提交于 2019-12-21 04:49:07
问题 I need to create some factories that are made of multiple has many through's Here are my models Topic has_many :plan_topics has_many :plans, :through => :plan_topics PlanTopic belongs_to :plan belongs_to :topic Plan has_many :subscriptions has_many :members, :through => :subscriptions has_many :plan_topics has_many :topics, :through => :plan_topics Subscription belongs_to :member belongs_to :plan Member has_many :subscriptions has_many :plans, :through => :subscriptions Here is what I have

factory_girl + rspec doesn't seem to roll back changes after each example

戏子无情 提交于 2019-12-20 17:34:23
问题 Similar to the problem described here: http://rpheath.com/posts/411-how-to-use-factory-girl-with-rspec in Short (shorten'd code): spec_helper: config.use_transactional_fixtures = true config.use_instantiated_fixtures = false factories.rb: Factory.define :state do f.name "NY" end in my spec before(:each) do @static_model = Factory(:state) # with validate uniqueness of state name end error: duplicate entry name "NY" etc. Question: Shouldn't rspec clear database before each spec example and

Testing simple STI with FactoryGirl

无人久伴 提交于 2019-12-20 11:06:28
问题 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, :name, :command # some methods I would like to test end The class CounterTask inherits from Task class CounterTask < Task end This all works fine until I am trying to test the base class, since it must have a type. FactoryGirl.define do factory :task do sequence(:name) { |n| "name_#{n}" } sequence(:command

How to mock and stub active record before_create callback with factory_girl

 ̄綄美尐妖づ 提交于 2019-12-20 10:14:38
问题 I have an ActiveRecord Model, PricePackage. That has a before_create call back. This call back uses a 3rd party API to make a remote connection. I am using factory girl and would like to stub out this api so that when new factories are built during testing the remote calls are not made. I am using Rspec for mocks and stubs. The problem i'm having is that the Rspec methods are not available within my factories.rb model: class PricePackage < ActiveRecord::Base has_many :users before_create

How to user factory girl to create associated lists with a has_many with a validation that requires it on create

梦想的初衷 提交于 2019-12-20 09:48:37
问题 In a Rails application, given three models User, Article and Reviewer with the following relationships and validations: class User < ActiveRecord::Base has_many :articles has_many :reviewers end class Reviewer < ActiveRecord::Base belongs_to :user belongs_to :article end class Article < ActiveRecord::Base belongs_to :user has_many :reviewers validate :has_reviewers? def has_reviewers? errors.add(:base, "article must have at least one reviewer.") if self.reviewers.blank? end end And the

Rails: Avoiding duplication errors in Factory Girl…am I doing it wrong?

我的未来我决定 提交于 2019-12-20 08:59:31
问题 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" error. I'm currently using a simple helper to search for an existing entry in the DB before creating the factory...and calling any factory I make through that helper. It works, but it's not entirely elegant, and considering how common I assume this problem must be, I'm guessing there's a better

ActiveModel::MissingAttributeError: can't write unknown attribute `ad_id' with FactoryGirl

大兔子大兔子 提交于 2019-12-18 18:36:33
问题 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 has_many :ads end class Image < ActiveRecord::Base belongs_to :page has_many :ads end And I have defined the following Factories: factory :page do url 'test.com' end factory :image do width 200 height 200 page end factory :ad do background 'rgb(255,0,0)' page image end When I try to do this: ad = FactoryGirl.create(:ad) I

ArgumentError: Factory not registered

泪湿孤枕 提交于 2019-12-17 22:27:19
问题 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

How do I simulate a login with RSpec?

隐身守侯 提交于 2019-12-17 17:37:26
问题 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