factory-bot

Get two associations within a Factory to share another association

点点圈 提交于 2019-12-03 09:57:17
I've got these 5 models: Guardian, Student, Relationship, RelationshipType and School. Between them, I've got these associations class Guardian < ActiveRecord::Base belongs_to :school has_many :relationships, :dependent => :destroy has_many :students, :through => :relationships end class Student < ActiveRecord::Base belongs_to :school has_many :relationships, :dependent => :destroy has_many :guardians, :through => :relationships end class Relationship < ActiveRecord::Base belongs_to :student belongs_to :guardian belongs_to :relationship_type end class School < ActiveRecord::Base has_many

How to use FactoryGirl to create an attribute called “alias”?

让人想犯罪 __ 提交于 2019-12-03 09:46:56
I'm just wondering whether it's possible to create an attribute called "alias" using FactoryGirl, since alias is a reserved word in Ruby. FactoryGirl.define do factory :blah do name "dummy" alias "dummy" end end I've tried various combinations of escaping things but can't get anything useful to work. Ruby doesn't know whether you're trying to call a method called alias or alias one method as another, and defaults to the latter. You can disambiguate by doing self.alias "dummy" ie, by explicitly specifying the receiver. This is usually the way to go in other cases where it is ambiguous whether

Rails How to use FactoryGirl in Seeds.rb?

北战南征 提交于 2019-12-03 08:40:45
I want to have the Seeds.rb file run a method in a file from the Rails.root.join('spec') directory which will fill the database with data generated by FactoryGirl. Lets call this file "helpers.rb" with the method "seed_data" Edit: require Rails.root.join('spec','helpers.rb') links the file. source This method I want to use in a before(:all) do seed_data end to seed the test database and rake db:seed for development/production databases with the same effect. Seeds.rb require Rails.root.join('spec','helpers.rb') require 'rubygems' #so it can load gems require 'factory_girl_rails' #so it can run

Rails 4 Paperclip FactoryGirl file uploading

元气小坏坏 提交于 2019-12-03 08:31:24
I have a FactoryGirl :product factory that uses fixture_file_upload to set image , which is a Paperclip attachment. image { fixture_file_upload "#{Rails.root}/spec/fixtures/images/product.png", 'image/png' } fixture_file_upload works fine, but every time a test creates a new Product using the factory, Paperclip creates a new file in publicproducts/<id>/original.png . This is the issue. . Filling a the folder publicproducts on each test run is not acceptable. The first workaround I can think of is the solution mentioned in https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Cleanup

FactoryGirl + Faker - same data being generated for every object in db seed data

孤者浪人 提交于 2019-12-03 08:10:45
问题 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 validation. Factory for users: #users.rb require 'faker' FactoryGirl.define do factory :user do first_name Faker::Name.first_name last_name Faker::Name.last_name phone Faker::PhoneNumber.cell_phone email Faker::Internet.email password "password" password_confirmation "password" end end And the code in

How to test model's callback method independently?

对着背影说爱祢 提交于 2019-12-03 07:28:00
问题 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 @article = FactoryGirl.create(:article) it "should work as expected" do @article.do_something expect(@article).to have_something end # ...several other examples for different cases end Everything was fine until I found it's better to move this method into a after_save callback: class Article < ActiveRecord:

How to include a module in a factory_girl factory?

我们两清 提交于 2019-12-03 05:17:40
I'm trying to reuse a helper method in all my factories, however I cannot get it to work. Here's my setup: Helper module (in spec/support/test_helpers.rb) module Tests module Helpers # not guaranteed to be unique, useful for generating passwords def random_string(length = 20) chars = ['A'..'Z', 'a'..'z', '0'..'9'].map{|r|r.to_a}.flatten (0...length).map{ chars[rand(chars.size)] }.join end end end A factory (in spec/factories/users.rb) FactoryGirl.define do factory :user do sequence(:username) { |n| "username-#{n}" } password random_string password_confirmation { |u| u.password } end end If I

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

旧街凉风 提交于 2019-12-03 04:57:12
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 hence not throwing duplicate entry errors? Things i think off: do you use rake spec to run your testsuite:

FactoryGirl + RSpec + Rails 3 'undefined method <attribute>='

橙三吉。 提交于 2019-12-03 04:53:49
问题 I'm fairly new to rails and TDD (as will no doubt be obvious from my post) and am having a hard time wrapping my brain around Rspec and FactoryGirl. I'm using Rails 3, rspec and factory girl: gem 'rails', '3.0.3' # ... gem 'rspec-rails', '~>2.4.0' gem 'factory_girl_rails' I have a user model that I've been successfully running tests on during development, but then needed to add an attribute to, called "source". It's for determining where the user record originally came from (local vs LDAP).

Problem with Factory_girl, association and after_initialize

瘦欲@ 提交于 2019-12-03 04:44:54
I have a Family class so defined: class Family < ActiveRecord::Base after_initialize :initialize_family belongs_to :user validates :user, :presence => true validates :name, :presence => true, :length => { :maximum => 30 }, :format => { :with => /\A[a-zA-Z0-9\-_\s\']+\z/i} def initialize_family if self.name.blank? && self.user self.name = "#{self.user.profile_full_name}'s Family" end end end In my factories.rb I have: Factory.define :family do |f| f.association :user, :factory => :user end In my family_spec.rb I have let(:family) { Factory(:family) } But this fails with: 1) Family is valid with