logo
down
shadow

Factory Girl pass parameter to trait


Factory Girl pass parameter to trait

By : Q_Abhishek
Date : November 21 2020, 11:01 PM
will help you You need to rethink your strategy a little here - you have a basic_instant_instance which does not inherit from instant_instance and therefore knows nothing about the trait [:active_now] or start attribute.
You also need to be evaluating the start_time at the time you are building the Factory instance by placing it in curly brackets. Otherwise it will be evaluated before start is initialised.
code :
FactoryGirl.define do

  factory :instant_instance do
    trait :active_now do 
      #attributes...

      transient do
        start nil
      end

      #overwrite start_time
      start_time { start.present? ? start : Time.now }
    end
  end
end
create( :instant_instance, start: 1.hours.ago, :active_now)


Share : facebook icon twitter icon
Trait not registered - Rspec - Factory Girl

Trait not registered - Rspec - Factory Girl


By : Siddharth Ramakrishn
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Not mentioned in my question, so this answer could not be deduced from my question, I post to complete.
Problem was I am using zeus server, I restarted zeus server and things worked.
Using factory girl to create a trait of has_many relationship

Using factory girl to create a trait of has_many relationship


By : Mittal Dhanani
Date : March 29 2020, 07:55 AM
To fix this issue To create a one-to-many relationship you could so something like this:
code :
factory :room do
  factory :bathroom do
    type :bathroom
  end
  factory :bedroom do
    type :bedroom
  end
end

factory :house do
  ignore do
    num_bathrooms 0
    num_bedrooms 0
  end

  trait :two_bathrooms do
    num_bathrooms 2
  end

  trait :three_bedrooms do
    num_bedrooms 3
  end

  after(:create) do |house, evaluator|
    create_list(:bathroom, evaluator.num_bathrooms, house: house)
    create_list(:bedroom, evaluator.num_bedrooms, house: house)
  end
end
Factory girl association with more than one trait

Factory girl association with more than one trait


By : Aiham Elias
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a association like this : , You could do this to have two different trait options:
code :
  factory :address do
    address1 "12 Any Street"
    latitude 22.4583397
    longitude -11.06776
    state 'pending_verification'

    trait :closer_address do
      latitude 33.4783397
      longitude -11.06776
    end

    trait :verified_closer_address do
      latitude 33.4783397
      longitude -11.06776
      state 'verified'
    end
  end
:factory => [:address, :closer_address]
:factory => [:address, :verified_closer_address] 
  factory :address do
    address1 "12 Any Street"
    latitude 22.4583397
    longitude -11.06776
    state 'pending_verification'

    trait :closer_address do
      latitude 33.4783397
      longitude -11.06776
    end

    trait :verified do
      state 'verified'
    end
  end
:factory => [:address, :closer_address, :verified]
factory girl, association in default trait, but leave it unassociated when trait is passed

factory girl, association in default trait, but leave it unassociated when trait is passed


By : Reino Ruusu
Date : March 29 2020, 07:55 AM
I hope this helps . FactoryGirl's transient attributes might be just what you need:
code :
FactoryGirl.define do
  factory :company do
    transient do
      create_address true
    end

    sequence(:name) { |n| "company #{n}" }
    description     { "description #{name}" }

    trait :no_address do
      transient do 
        create_address false
      end
    end

    after(:create) do |company, evaluator|
      create_list(:address, 3, company: company) if evaluator.create_address
    end
  end
end
Factory Girl: Specify Trait for Parent

Factory Girl: Specify Trait for Parent


By : user3894336
Date : March 29 2020, 07:55 AM
it should still fix some issue I would like a factory to inherit from another factory with a specific trait. How can I specify a parent factory with a specific trait as the parent? I'm looking for something along the lines of how you can declare an association with a specific trait, like this: , If I'm not mistaken, this should work:
code :
factory :mom do
    trait :smart do
    end
  end

factory :kiddo, parent: :mom, traits: [:smart] do
end
Related Posts Related Posts :
  • Rails show total column when data comes from model method
  • Remove command injection in rails
  • undefined method `[]' for nil:NilClass + after login
  • Rails multiply joined columns with foreign key null
  • How to get .last object according to .id?
  • Rails how to make example.com/post/1 to example.com/blog/post/1
  • Referencing a table twice in rails/postgres
  • Error: Could not find a valid gem installing rails on mac
  • How to undo the action of rails db:migrate in Rails 5.0.0.1
  • Rails 5 - include nested associations not working
  • Rails changing variable name in for loop
  • Why can't I install rails?
  • Array not saving user input in ruby
  • node.trigger("click") - Capybara
  • Doing Subtracting on my Rails App with user input
  • Angular on Rails API: HTTP Request
  • How to define dynamic variables on Graphql?
  • Doing multiplication on my Rails App with user input
  • Converting integer type to string rails
  • Extremely slow PUT request in Redmine with plugins
  • find model class of array in hash rails 4
  • Minitest stub_any_instance and methods?
  • Added Column Not accessible in next migration file
  • Not able to connect mongodb with Rails container using Docker compose
  • How to properly load lib modules and classes in Rails 5 app
  • Ruby on Rails, taking data from Graphql and posting on it to Facebook-messenger
  • How to define let! with mutliple objects in Rspec test?
  • How can I prepare my rails app for production?
  • Rails 5 display number of results when grouped
  • Skipping Active Record breaks Rails Generator
  • Split youtube url rails
  • Rails root test
  • In Ruby, What exactly is the difference between RVM, Bundler and Rake?
  • 403 Forbidden - Nginx + Passenger - Rails
  • RSpec Load specific seed data for specific tests
  • How to update Ruby 2.2.4 to 2.2..5
  • Rails Form Saving the Wrong Date via `datetime_select`
  • Undefined method 'permit' for "89718":String
  • How do I prevent my "production" group from being called when running in my Rails development enviornment?
  • Save instance variable to database Rails
  • Rails: Nested form_for Errors: 'ActionController::UrlGenerationError'
  • Ruby on Rails - undefined method `errors' for nil:NilClass
  • error: failed to push some refs to 'https://git.heroku.com/radiant-chamber-18560.git'
  • Uploading/playing mp3 files using Rails
  • Heroku Everything Up-To-Date
  • How to send mails automatically using whenever gem
  • How to reduce external API calls in rails
  • Having trouble using where.not in rails
  • How to keep attributes from two different tables synchronized in RoR?
  • Reset scopes set in config/initializers/doorkeeper.rb on Heroku
  • Rails Console Showing Date Only for Datetime Column
  • Why doesn't my second validation email work?
  • Rails/Ruby incorrectly showing variable not defined
  • Does attr_accessor = nil after the page is redirected?
  • Can't access aws public ip via deploy or ping
  • Puma is not working on server
  • Iterate records in rails view and controller
  • Recover or reset local login information managed by the Devise gem
  • Rake / adapter error during Rails 5 app deploy on Bluemix
  • How to catch pagination element of shopify api with jquery
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com