factory-bot

Build factories for self referential associations in rails

戏子无情 提交于 2019-12-23 04:13:11
问题 I have a typical requirement, I have to address user object as follows user.referrer and user.referrers. Basically, user can refer more than one person and one person should be referred by one particular user. So I build associations as follows. They are working great. class User < ActiveRecord::Base attr_accessible :account_number, :display_name, :referrer_id has_many :referrers, :class_name => "User", :foreign_key => "referrer_id" belongs_to :referrer, :class_name => "User" end Now I would

Factory Girl - Why are Records being continually created?

╄→гoц情女王★ 提交于 2019-12-23 01:45:42
问题 I'm new to factory girl. What I'm trying to do is create 2 users, which belong to a group, joined by the permission model. Here's what I have. When I run this one rspec, it creates more than 2 users, 4+. Why? thanks factories.rb: require 'factory_girl' Factory.define :user do |f| f.sequence(:fname) { |n| "fname#{n}" } f.sequence(:lname) { |n| "lname#{n}" } f.sequence(:email) { |n| "email#{n}@google.com" } f.password "password" f.password_confirmation { |u| u.password } f.invitation_code

Factory Girl with enum and association

笑着哭i 提交于 2019-12-23 00:52:10
问题 I have a model that uses both an association and an enum attribute. class ProjectItem < ActiveRecord::Base belongs_to :project enum status: {open: 0, pending: 1, completed: 2} When I'm running a test on the create action of a model with an association, I use build(:model_name).attributes like this: it "creates a new ProjectItem" do expect { post :create, document_project_item: build(:project_item).attributes }.to change(ProjectItem, :count).by(1) end This was failing, and I found this issue

Factory Girl - has many associations

[亡魂溺海] 提交于 2019-12-22 08:11:54
问题 I'm using factory girl with rspec, here is what I have: factories.rb Factory.define :user do |f| f.sequence(:fname) { |n| "fname#{n}" } f.sequence(:lname) { |n| "lname#{n}" } f.sequence(:email) { |n| "email#{n}@google.com" } f.password "password" f.password_confirmation { |u| u.password } f.invitation_code "xxxxxxx" end Factory.define :group do |f| f.sequence(:name) { |n| "myGroup#{n}" } f.sequence(:private_email) { |n| "myGroup#{n}" } end Factory.define :permission do |f| f.role_id 1 end

Factory_girl has_one relation with validates_presence_of

好久不见. 提交于 2019-12-22 04:16:07
问题 I have 2 Models: # user.rb class User < ActiveRecord::Base has_one :profile, :dependent => :destroy end # profile.rb class Profile < ActiveRecord::Base belongs_to :user validates_presence_of :user end # user_factory.rb Factory.define :user do |u| u.login "test" u.association :profile end I want to do this: @user = Factory(:user) => #<User id: 88,....> @user.profile => #<Profile id:123, user_id:88, ......> @user = Factory.build(:user) => #<User id: nil,....> @user.profile => #<Profile id:nil,

FactoryGirl: Factory not registered

旧时模样 提交于 2019-12-22 01:38:17
问题 I work on a project with a structure like: projects/warehouse/core projects/warehouse/products/bottles projects/warehouse/products/boxes In this project, the application logic, gems, etc. are all in the core application. I have boxes set up for rspec like such: projects/warehouse/products/boxes/spec /factories /models The factories directory contains cubics.rb : FactoryGirl.define do factory :cubic id 1 dimension 12 end end The models directory contains cubic_spec.rb : require 'spec_helper'

Rails Model Testing - Mocking vs. Factories

安稳与你 提交于 2019-12-21 17:33:11
问题 What's the best practice for Rails testing for mocking objects vs. using factory objects. Should mocking only be used when a model might go to an external source? Or do you only use factories when you're testing the actual model and using mocking for everything else. For example if we have a sales system with customers and orders when we test the customer models do we mock the order or do we just use a factory order? Does it even make a difference? 回答1: We've had this debate often at our web

Factory Girl and has_one

风格不统一 提交于 2019-12-21 13:08:18
问题 Here's my models : Class Audition belongs_to :video end Class Video has_one :audition end and my factories : Factory.define :video do |v| v.filename {Sham.filename} v.video_url {Sham.url} end Factory.define :audition do |a| a.video {|a| a.association(:video)} a.label {Sham.label} end How could I create a video factory that have an audition, I mean, be able to : v = Factory.create(:video) v.audition # I'd like this to be not nil ! Because I have an observer on my video that try to access the

undefined method `create' rails spec.

不问归期 提交于 2019-12-21 07:55:20
问题 I have installed factory girl and trying to use it with spec. scenario 'User signs in' do create :user, email: 'test@example.com', password: 'testpassword' visit '/users/sign_in' fill_in 'Email', with: 'test@example.com' fill_in 'Password', with: 'testpassword' end and I am getting the following error. Failure/Error: create :user, email: 'test@example.com', password: 'testpassword' NoMethodError: undefined method `create' for #<RSpec::ExampleGroups::UserSignIn:0x007fe6324816b8> 回答1: We can

Factory Girl / Capybara deleting records from database mid-test?

时光毁灭记忆、已成空白 提交于 2019-12-21 07:09:36
问题 Working with RSpec & Capybara, I'm getting an interesting test failure mode which goes away with a few subtle rearrangements of lines in the test case...stuff that shouldn't matter. I'm developing my own authentication system. It is currently working and I can login/out with the browser and the session works etc etc. However, trying to test this is failing. Something is going on that I don't quite understand, which seems to depend on the order of (seemingly) unrelated calls. require 'spec