factory-bot

Faker is producing duplicate data when used in factory_girl

混江龙づ霸主 提交于 2019-11-28 04:26:51
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: "Bailey", last_name: "Durgan"> >> Factory(:user) => #<User id: 17, email: "user8@blow.com", created_at:

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

戏子无情 提交于 2019-11-28 03:23:34
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 together in pieces. I had to do this to get around the problem. This still demonstrates the problem though

How to set up factory in FactoryGirl with has_many association

主宰稳场 提交于 2019-11-28 03:22:13
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, :through => :listing_features validates_presence_of ... ... end class Feature < ActiveRecord::Base attr_accessible ... validates_presence_of ... validates_uniqueness_of ... has_many :listing_features has_many :listings, :through => :listing_features end class ListingFeature < ActiveRecord::Base attr_accessible :feature_id, :listing_id belongs_to :feature

Skip callbacks on Factory Girl and Rspec

拈花ヽ惹草 提交于 2019-11-28 03:07:35
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 I'm not sure if it is the best solution, but I have successfully achieved this using: FactoryGirl.define do factory :user do first_name "Luiz" last_name "Branco" #... after(:build) { |user| user.class.skip_callback(

How to create has_and_belongs_to_many associations in Factory girl

[亡魂溺海] 提交于 2019-11-28 02:53:16
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 association? Here's my attempt Factory.define :company do |f| f.users{ |users| [users.association :company]} end Factory.define :user do |f| f.companies{ |companies| [companies.association :user]} end now I try Factory :user Perhaps unsurprisingly this results in an infinite loop as the factories recursively use each other to define themselves. More

Rspec and Rails 4, update skip callback

血红的双手。 提交于 2019-11-28 01:46:34
I try to run an update test with rspec for my rails application. I'm using rspec 3.5 and rails 4. The behaviour is supposed to be the following : When i create a new service with a selling price, it's create a Price instance and set the relation with the service. Then, when i update my service, if there is no selling price, it's destroy the price record (requirement of the client to save space in database). The process i implemented seems to be working fine, when i test with the UI and i check the count of Price record, it's decrease by one like it's suppose. However, the unit test if failing.

how to define factories with a inheritance user model

痴心易碎 提交于 2019-11-27 22:59:57
问题 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 :customuserfield... end How can i write factories to map this Class hirachie in my specs. And keep up writing with don´t repeat yourself. FactoryGirl.define do factory :user do name "name" bla "bla" factory :custom_user do customfield "customfield" end end end This doesn´t work for me because the class is also

How to set up factory in FactoryGirl with has_many association

▼魔方 西西 提交于 2019-11-27 19:14:05
问题 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, :through => :listing_features validates_presence_of ... ... end class Feature < ActiveRecord::Base attr_accessible ... validates_presence_of ... validates_uniqueness_of ... has_many :listing_features has_many :listings, :through => :listing_features end

FactoryGirl screws up rake db:migrate process

空扰寡人 提交于 2019-11-27 18:00:36
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 end If I drop, create then migrate the database in the test enviroment I get this error: rake aborted! Could not find table 'categories' This problem occurs because FactoryGirl expects the tables to already exist (for some odd reason). If I remove the spec folder from my rails app and do db:migrate , it works. Also if I mark factory-girl-rails from my Gemfile as :require => false it also works (then I have

How can I reset a factory_girl sequence?

纵然是瞬间 提交于 2019-11-27 17:11:44
问题 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} subtitle" } p.sequence(:image) { |n| "../images/content/projects/#{n}.jpg" } p.sequence(:date) { |n| n.weeks.ago.to_date } end And that I'm creating instances of project Factory.build :project Factory.build :project By this time, the next time I execute Factory.build(:project) I'll receive an instance of Project with a title set to