factory-bot

Get two associations within a Factory to share another association

拈花ヽ惹草 提交于 2019-12-04 16:55:29
问题 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

rails factory girl getting “Email has already been taken”

江枫思渺然 提交于 2019-12-04 16:52:38
问题 This is my factory girl code, and every time I try to generate a review, it's telling me that "Email has already been taken", i've reset my databases, set the transition in spec_helper to true, but still haven't solved the problem. I'm new to this, am I using the association wrong? Thanks! Factory.define :user do |user| user.name "Testing User" user.email "test@example.com" user.password "foobar" user.password_confirmation "foobar" end Factory.define :course do |course| course.title "course"

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

心已入冬 提交于 2019-12-04 15:40:26
问题 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. 回答1: 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

Rails 4 Paperclip FactoryGirl file uploading

为君一笑 提交于 2019-12-04 13:18:18
问题 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

FactoryGirl creates user, but save point is released before test starts

百般思念 提交于 2019-12-04 12:44:58
I am running rspec tests for spec/requests/user_pages_specs: require 'spec_helper' describe "User pages" do subject { page } describe "home page" do before { visit root_path } it { should have_content('Sign up with Facebook') } it { should have_title(full_title('')) } end describe "profile page" do let(:user) { FactoryGirl.create(:user) } before { visit user_path(user.id) } it { should have_content(user.name) } it { should have_title(user.name) } end end As you can see, I am using FactoryGirl to create a user. When I run the same command (i.e. FactoryGirl.create(:user) ) is rails console test

when does factory girl create objects in db?

浪子不回头ぞ 提交于 2019-12-04 08:53:12
问题 I am trying to simulate a session using FactoryGirl / shoulda (it worked with fixtures but i am having problems with using factories). I have following factories (user login and email both have unique validations): Factory.define :user do |u| u.login 'quentin' u.email 'quentin@example.com' end Factory.define :session_user, :class => Session do |ses| ses.association :user, :factory => :user ses.session_id 'session_user' end and here's the test class MessagesControllerTest < ActionController:

Machinist vs FactoryGirl - pros and cons

冷暖自知 提交于 2019-12-04 08:48:01
问题 I'm working with factory_girl, but looking at the machinist gem. Could you tell me please - what are the pros and cons of migrating to machinist? Have you compared those libs? 回答1: Machinist was actually heavily inspired by factory_girl, but varied because machinist's author wanted a different syntax. Since then, factory_girl added different syntax layers to simulate other factory libraries (including machinist's "blueprint" syntax). In other words, both are extremely similar, just with a

FactoryGirl: Populate a has many relation preserving build strategy

六月ゝ 毕业季﹏ 提交于 2019-12-04 08:35:24
问题 My problem seems very common, but I haven't found any answer in the documentation or the internet itself. It might seem a clone of this question has_many while respecting build strategy in factory_girl but 2,5 years after that post factory_girl changed a lot. I have a model with a has_many relation called photos. I want to populate this has many relation preserving my choice of build strategy. If I call offering = FactoryGirl.build_stubbed :offering, :stay I expect offering.photos to be a

How to make has_many :through association with fixtures?

旧时模样 提交于 2019-12-04 06:15:36
问题 I can't use factory_girl because I'm testing sunspot and need real database. Edit : nope. It can works with sunspot. I'm wrong. How can I build has_many :through(a.k.a many-to-many) associations in fixtures? I google it and get a invalid solution Edit : Finally I use factory_girl. I google-copy-paste a snippet: factory :tagging do question { |a| a.association(:question) } tag { |a| a.association(:tag) } end (question has_many tags through taggings, vice versa) It works well. But what's it?

Factory Girl and has_one

戏子无情 提交于 2019-12-04 05:24:58
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 audition from the video object I tried several things but I always end with a stack level too deep or