factory-bot

Creating a Factory association that defaults to nil?

久未见 提交于 2020-01-04 06:53:48
问题 Using the FactoryGirl gem, inside the factories.rb file, how can I create a factory with an association that defaults to nil? I am thinking something along these lines: Factory.define :user do |factory| factory.association :post factory.association :comment, :default => nil end Would that be right and would that be ok to do? 回答1: Factory.define :user do |factory| factory.association :post factory.comment_id nil end 回答2: FactoryGirl now benefits from a :null strategy. Therefore, you can define

How to create a user record using factory girl and cucumber?

对着背影说爱祢 提交于 2020-01-04 06:49:27
问题 I have never used factory girl and I started testing from this week so all this new stuff just making me crazy. Can anyone explain me a simple way to create a record using factory girl. Here is the use case. I already created a test using cucumber where it creates a user and also create other records like account, user categories. Now I am writing second test case where I say there should be no test data(I can use data created from first test but dont want dependency from other test cases.).

Factory Girl with has many relationship (and a protected attribute)

我是研究僧i 提交于 2020-01-04 04:02:44
问题 I have this kind of relation: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end The default scenario inside controllers looks like: @article = Article.create(:title => "foobar") @comment = @article.comments.create(:content => "w00t") I had tried to write those factories: Factory.define :article do |f| f.title "Hello, world" end Factory.define :comment do |f| f.content "Awesome!" f.association

Factory Girl with has many relationship (and a protected attribute)

我的梦境 提交于 2020-01-04 04:02:44
问题 I have this kind of relation: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end The default scenario inside controllers looks like: @article = Article.create(:title => "foobar") @comment = @article.comments.create(:content => "w00t") I had tried to write those factories: Factory.define :article do |f| f.title "Hello, world" end Factory.define :comment do |f| f.content "Awesome!" f.association

Difference between factory_girl and factory_girl_rails Ruby Gems

北慕城南 提交于 2020-01-03 05:18:31
问题 Is there a difference between factory_girl and factory_girl_rails Ruby Gems? I have a recurring problem with an error in RSpec Testing: "uninitialized constant FactoryGirl (NameError)". Somebody told me that there is a difference between the two (this is really confusing) and one needs the other to work or something along those lines? My spec_helper file has both: require 'factory_girl' require 'factory_girl_rails' My Gemfile has: gem 'factory_girl_rails' Here is the full error: uninitialized

How to Populate Lookup tables in Testing (Rails)

孤街醉人 提交于 2020-01-02 05:06:10
问题 I am using Cucumber, Rspec, and Factory Girl for the testing of my Rails Application. But I have several lookup tables that contain mostly static data. So I'm trying to figure out the best way to populate these when testing. Doing them individually in FactoryGirl seems tedious and I'd like to stay away from Fixtures. For development and production, I populate them in my seeds.rb file. Thanks! 回答1: Use Factory Girl .sequence, Populator and Faker and you'll never run out of lab rats! Factory

Reload factory_girl factory

独自空忆成欢 提交于 2020-01-01 08:23:09
问题 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 回答1: You

factory girl uniqueness validation fails for associated factories

不问归期 提交于 2020-01-01 08:15:06
问题 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

Rails How to use FactoryGirl in Seeds.rb?

与世无争的帅哥 提交于 2020-01-01 03:44:05
问题 I want to have the Seeds.rb file run a method in a file from the Rails.root.join('spec') directory which will fill the database with data generated by FactoryGirl. Lets call this file "helpers.rb" with the method "seed_data" Edit: require Rails.root.join('spec','helpers.rb') links the file. source This method I want to use in a before(:all) do seed_data end to seed the test database and rake db:seed for development/production databases with the same effect. 回答1: Seeds.rb require Rails.root

Spork and cache_classes problem with rspec, factory_girl and datamapper

邮差的信 提交于 2020-01-01 02:33:07
问题 I've got a problem with Spork test server. If I set config.cache_classes = false in config/environments/test.rb then specs start to rasie errors. Failure/Error: task = Factory(:something, :foo => @foo, :bar => @bar) DataMapper::ImmutableError: Immutable resource cannot be modified This is my spec_helper.rb: require 'spork' Spork.prefork do if ENV['CODE_COVERAGE'] == '1' require 'simplecov' SimpleCov.start 'rails' end ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config