What is the purpose of a `transient do` block in FactoryBot factories?

transient attributes allow you to pass in data that isn’t an attribute on the model. Say you have a model called car with the following attributes: name purchase_price model You want to capitalize the name of the car when you create the car model in the factory. What we can do is: factory :car do … Read more

Factory-girl create that bypasses my model validation

This isn’t very specific to FactoryGirl, but you can always bypass validations when saving models via save(validate: false): describe “.current” do let!(:current_group) { FactoryGirl.create(:group) } let!(:old_group) do g = FactoryGirl.build(:group, expiry: Time.now – 3.days) g.save(validate: false) g end specify { Group.current.should == [current_group] } end

How to set up factory in FactoryBot with has_many association

Alternatively, you can use a block and skip the association keyword. This makes it possible to build objects without saving to the database (otherwise, a has_many association will save your records to the db, even if you use the build function instead of create). FactoryGirl.define do factory :listing_with_features, :parent => :listing do |listing| features { … Read more

What’s the difference between the build and create methods in FactoryGirl?

The create() method persists the instance of the model while the build() method keeps it only on memory. Personally, I use the create() method only when persistence is really necessary since writing to DB makes testing time consuming. e.g. I create users to authentication with create() because my authentication engine queries the DB. To check … Read more

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

You can use fixture_file_upload include ActionDispatch::TestProcess in your test helper, here is an example factory: include ActionDispatch::TestProcess FactoryBot.define do factory :user do avatar { fixture_file_upload(Rails.root.join(‘spec’, ‘photos’, ‘test.png’), ‘image/png’) } end end In the above example, spec/photos/test.png needs to exist in your application’s root directory before running your tests. Note, that FactoryBot is a new name … Read more

Skip callbacks on Factory Girl and Rspec

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(:create, :after, :run_something) } factory :user_with_run_something do after(:create) { |user| user.send(:run_something) } end end end Running without callback: FactoryGirl.create(:user) Running with callback: FactoryGirl.create(:user_with_run_something)

How to create has_and_belongs_to_many associations in Factory girl

Here is the solution that works for me. FactoryGirl.define do factory :company do #company attributes end factory :user do companies {[FactoryGirl.create(:company)]} #user attributes end end if you will need specific company you can use factory this way company = FactoryGirl.create(:company, #{company attributes}) user = FactoryGirl.create(:user, :companies => [company]) Hope this will be helpful for somebody.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)