factory-bot

How to create has_and_belongs_to_many associations in Factory girl

故事扮演 提交于 2019-11-26 23:56:22
问题 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

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

妖精的绣舞 提交于 2019-11-26 22:15:26
问题 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

Rspec and Rails 4, update skip callback

拈花ヽ惹草 提交于 2019-11-26 21:59:43
问题 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

FactoryGirl: why does attributes_for omit some attributes?

戏子无情 提交于 2019-11-26 16:43:26
问题 I want to use FactoryGirl.attributes_for in controller testing, as in: it "raise error creating a new PremiseGroup for this user" do expect { post :create, {:premise_group => FactoryGirl.attributes_for(:premise_group)} }.to raise_error(CanCan::AccessDenied) end ... but this doesn't work because #attributes_for omits the :user_id attribute. Here is the difference between #create and #attributes_for : >> FactoryGirl.create(:premise_group) => #<PremiseGroup id: 3, name: "PremiseGroup_4", user_id