factory-bot

Rails FactoryGirl trait association with model after_create callback not setting vanity_url

▼魔方 西西 提交于 2019-12-04 04:52:38
问题 In my model, I have an after_create callback that triggers the method: class Job < ActiveRecord::Base after_create :update_vanity_url private def update_vanity_url self.vanity_url = '/jobs/' + company.slug + '/' + slug + '/' + id.to_s + '/' end end This sets up a custom url for my jobs, however, when I try to use this in my coupon factory it is not being saved. The coupon is created without a job assigned. Only when the coupon is used is it paired with one job. I referred to that as executed:

Reload factory_girl factory

落花浮王杯 提交于 2019-12-04 02:22:03
I want that when I make changes in factories, I see them in rails console without restarting all console. I've found some lines and tested them with a poor understanding of what's going on, documentation isn't clear for me.: FactoryGirl.reload I've also tested: > FactoryGirl.factories.clear > FactoryGirl.find_definitions # also tested FactoryGirl.factories.find_definitions # but NoMethodError is raised => ActiveRecord::RecordNotFound: Couldn't find Address with ID=277 Torphy Squares hjblok You can also use reload! to reload the console environment but it doesn't reload the factories. Use

How to resolve factory_girl wrong number of arguments error

£可爱£侵袭症+ 提交于 2019-12-04 02:12:05
#rspec test code @room = FactoryGirl.build(:room) #factory definition factory :room do length {10} width {20} end #code implementation class Room attr_accessor :length, :width def initialize(length,width) @length = length @width = width end end Running rspec results in this error when trying to build the @room ArgumentError: wrong number of arguments (0 for 2) FactoryGirl does not currently support initializers with arguments. So it fails when it's trying to do Room.new when you run build . One simple workaround for this might be to monkey-patch your classes in your test setup to get around

factory girl uniqueness validation fails for associated factories

半世苍凉 提交于 2019-12-04 00:38:15
I have (simplified) factories defined as follows: factory :league do acronym 'NBA' end factory :division do league end Divisions belong to Leagues. When I define this factory, it was my assumption that 1 league would be created, and that league would be reused over and over again to give divisions a real league_id. Instead, I'm getting errors on the 2nd call of FactoryGirl.create(:division) because the League acronym is supposed to be unique. class League < ActiveRecord::Base validates :acronym, uniqueness: true end leading to the following break in the test ActiveRecord::RecordInvalid:

undefined method `create' rails spec.

一笑奈何 提交于 2019-12-04 00:13:59
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> We can find solution in factory_bot's documentation : 1) Create file /spec/support/factory_bot.rb : RSpec.configure

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

半城伤御伤魂 提交于 2019-12-03 23:39:30
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_helper' describe "Sessions" do it 'allows user to login' do #line one user = Factory(:user) #For SO, this

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

天涯浪子 提交于 2019-12-03 22:05:52
What is the purpose of transient do in FactoryBot factories? I've seen a lot of factories that begin with something like below. factory :car do owner nil other_attribute nil end ... I've found some information on this blog: http://blog.thefrontiergroup.com.au/2014/12/using-factorygirl-easily-create-complex-data-sets-rails/ But I still don't fully understand how and why to do this. My experience with FactoryBot is minimal. Could anyone with some experience using FactoryBot share some insight? transient attributes allow you to pass in data that isn’t an attribute on the model. Say you have a

Using factory_girl_rails with Rspec on namespaced models

橙三吉。 提交于 2019-12-03 22:04:20
I have a web service that serves Ads to several different clients. The structure of the Ad varies between clients, and therefore, I am using namespaces for my models and controllers by the client name to differentiate between Ads. From the high level, it looks like this: 'app/models/client1/ad.rb' class Client1::Ad < ActiveRecord::Base attr_accessible :title, :description end 'app/models/client2/ad.rb' class Client2::Ad < ActiveRecord::Base attr_accessible :title, :description, :source end In reality, these models are more complex and have associations, but that is not the point. I am writing

How do I create an association with a has_many :through relationship in Factory Girl?

☆樱花仙子☆ 提交于 2019-12-03 21:11:33
In my models I have the following setup: class User < ActiveRecord::Base has_many :assignments has_many :roles, :through => :assignments end class Role < ActiveRecord::Base has_many :assignments has_many :users, :through => :assignments end class Assignment < ActiveRecord::Base belongs_to :user belongs_to :role attr_accessible :role_id, :user_id end In my factory.rb file I have: FactoryGirl.define do factory :user do sequence(:username) { |n| "user#{n}" } email { "#{username}@example.com" } password 'secret' password_confirmation 'secret' factory :admin do ... end end factory :role do name

How can I build/create a many-to-many association in factory_girl?

我们两清 提交于 2019-12-03 20:34:48
I have a Person model that has a many-to-many relationship with an Email model and I want to create a factory that lets me generate a first and last name for the person (this is already done) and create an email address that is based off of that person's name. Here is what I have for create a person 's name: Factory.sequence :first_name do |n| first_name = %w[FirstName1 FirstName2] # ... etc (I'm using a real subset of first names) first_name[(rand * first_name.length)] end Factory.sequence :last_name do |n| last_name = %w[LastName1 LastName2] # ... etc (I'm using a real subset of last names)