factory-bot

FactoryGirl association model trouble: “SystemStackError: stack level too deep”

柔情痞子 提交于 2019-11-27 16:19:37
问题 I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I am trying to state a Factory association model but I am in trouble. I have a factories/user.rb file like the following: FactoryGirl.define do factory :user, :class => User do attribute_1 attribute_2 ... association :account, :factory => :users_account, :method => :build, :email => 'foo@bar.com' end end and a factories/users/account.rb file like the following: FactoryGirl.define do factory :users_account, :class => Users::Account

has_many while respecting build strategy in factory_girl

不羁的心 提交于 2019-11-27 14:45:25
问题 Situation # Models class User < ActiveRecord::Base has_many :items end class Items < ActiveRecord::Base belongs_to :user validates_presence_of :user_id end # Factories Factory.define(:user) do |u| u.name "foo" end Factory.define(:user_with_items, :parent => :user) do |u| u.items {|items| [items.association(:item), items.association(:item)]} end Factory.define(:item) do |i| i.color "red" end Factory.define(:item_with_user, :parent => :user) do |i| i.association(:user) end Problem If you run

factory girl nested factory

淺唱寂寞╮ 提交于 2019-11-27 13:08:14
问题 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

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

自古美人都是妖i 提交于 2019-11-27 06:03:30
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 ... end Person is required to have at least one Image attached to it. When using FactoryGirl, I have code akin to the following: Factory.define :image do |a| a.data { File.new(File.join(Rails.root, 'features', 'support', 'file.png')) } a.association :person end Factory.define :person do |p| p.first_name 'Keyzer' p.last_name 'Soze' p.after_create do |person| person

Skip callbacks on Factory Girl and Rspec

杀马特。学长 韩版系。学妹 提交于 2019-11-27 05:05:51
问题 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 ... end Factory: FactoryGirl.define do factory :user do first_name "Luiz" last_name "Branco" ... # skip callback factory :with_run_something do # run callback end end 回答1: I'm not sure if it is the best solution, but I have successfully achieved this using: FactoryGirl.define do

FactoryGirl has_many association with validation

本小妞迷上赌 提交于 2019-11-27 01:50:20
问题 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

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

三世轮回 提交于 2019-11-27 01:35:14
问题 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

FactoryGirl: attributes_for not giving me associated attributes

孤人 提交于 2019-11-27 01:15:33
问题 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 controller with a simple test_should_create_code like this: test "should create code" do assert_difference('Code.count') do post :create, :code => Factory.attributes_for(:code) end assert_redirected_to code_path(assigns(:code)) end ... the test fails. The new record is not created. In the console, it seems that

Faker is producing duplicate data when used in factory_girl

旧巷老猫 提交于 2019-11-27 00:32:22
问题 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" } end However while I expect this to produce users who have different first_name and last_names, each one is the same: >> Factory(:user) => #<User id: 16, email: "user7@blow.com", created_at: "2011-03-18 18:29:33", updated_at: "2011-03-18 18:29:33", first_name:

Using factory_girl in Rails with associations that have unique constraints. Getting duplicate errors

让人想犯罪 __ 提交于 2019-11-27 00:02:00
问题 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 tables with lookup data. When I create a Cart with two products that have the same product type, each created product is re-creating the same product type. This errors from a unique validation on the ProductType model. Problem Demonstration This is from a unit test where I create a Cart and put it