factory-bot

Undefined methods for FactoryGirl transient variables

£可爱£侵袭症+ 提交于 2019-12-01 02:23:01
I'm working with FactoryGirl in version 4.4 . I'm trying to create after(:create) callbacks but I'm failing on evaluator variables. FactoryGirl.define do factory :organization do name 'example' factory :organization_with_links do transient do links_count 5 end after(:create) do |organization, evaluator| create_list(:link, evaluator.links_count, organization: organization) end end end end unfortunately I got NoMethodError: undefined method `links_count' for FactoryGirl::SyntaxRunner:0x007fcefb1ff780> according to officall factory girl guide I'm doing everything properly : https://github.com

How can I clean my database between erroneous rspec specs?

房东的猫 提交于 2019-12-01 02:18:05
问题 I have added the database_cleaner gem to my rails application in order to clean my database between specs. Here's my current configuration for database_cleaner, located in spec/spec_helper.rb : config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) DatabaseCleaner.start DatabaseCleaner.clean end config.before(:each) do DatabaseCleaner.clean end config.after(:each) do DatabaseCleaner.clean end config.after(:suite) do DatabaseCleaner.clean end

Convert fixtures into Factory Girl in Rails

回眸只為那壹抹淺笑 提交于 2019-11-30 22:04:09
I'd like to migrate my fixtures to "Factory Girl" in Rails. Is there any easy way to convert all yml files in a factories.rb file? I am assuming what you are looking to do is find a script which will look through your models and generate the factories for them. I tried this once (with something other than factory_girl) and found it full of bad data. I would suggest that you slowly transition to using factories. As you write new tests or update old ones, create the necessary factories. As time goes by, if you have the time and energy you can then choose an individual test case and replace all

Undefined methods for FactoryGirl transient variables

蹲街弑〆低调 提交于 2019-11-30 21:57:35
问题 I'm working with FactoryGirl in version 4.4 . I'm trying to create after(:create) callbacks but I'm failing on evaluator variables. FactoryGirl.define do factory :organization do name 'example' factory :organization_with_links do transient do links_count 5 end after(:create) do |organization, evaluator| create_list(:link, evaluator.links_count, organization: organization) end end end end unfortunately I got NoMethodError: undefined method `links_count' for FactoryGirl::SyntaxRunner

Cannot get factory_girl running under rails 3.0.5,unexpected tCONSTANT

心不动则不痛 提交于 2019-11-30 18:35:08
This is my Gemfile config: group :development, :test do gem 'rspec-rails' gem 'factory_girl', '~>2.0.0.beta1' gem 'factory_girl_rails', :git => 'https://github.com/thoughtbot/factory_girl_rails.git', :tag => 'v1.1.beta1' end This is my spec_helper.rb : # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require "factory_girl" # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join(

Convert fixtures into Factory Girl in Rails

浪尽此生 提交于 2019-11-30 17:53:14
问题 I'd like to migrate my fixtures to "Factory Girl" in Rails. Is there any easy way to convert all yml files in a factories.rb file? 回答1: I am assuming what you are looking to do is find a script which will look through your models and generate the factories for them. I tried this once (with something other than factory_girl) and found it full of bad data. I would suggest that you slowly transition to using factories. As you write new tests or update old ones, create the necessary factories. As

ActiveModel::MissingAttributeError: can't write unknown attribute `ad_id' with FactoryGirl

依然范特西╮ 提交于 2019-11-30 16:59:55
I have the following models: class Ad < ActiveRecord::Base belongs_to :page has_one :image has_one :logo end class Page < ActiveRecord::Base has_many :logos has_many :images has_many :ads end class Image < ActiveRecord::Base belongs_to :page has_many :ads end And I have defined the following Factories: factory :page do url 'test.com' end factory :image do width 200 height 200 page end factory :ad do background 'rgb(255,0,0)' page image end When I try to do this: ad = FactoryGirl.create(:ad) I get the following error ActiveModel::MissingAttributeError: can't write unknown attribute ad_id' right

Setting roles through rolify in FactoryGirl definition

依然范特西╮ 提交于 2019-11-30 11:49:47
问题 I am using devise, rolify and cancan. I'm also using rspec with FactoryGirl for testing. Right now I'm working on some tests and I want to define users with different roles for those tests. Here is my current guess for how to do that using FactoryGirl: FactoryGirl.define do factory :user do name 'Test User' email 'example@example.com' password 'please' password_confirmation 'please' # required if the Devise Confirmable module is used confirmed_at Time.now factory :admin do self.has_role

How to complete the rspec put controller test from scaffold

蓝咒 提交于 2019-11-30 11:41:50
问题 I'm using scaffolding to generate rspec controller tests. By default, it creates the test as: let(:valid_attributes) { skip("Add a hash of attributes valid for your model") } describe "PUT update" do describe "with valid params" do let(:new_attributes) { skip("Add a hash of attributes valid for your model") } it "updates the requested doctor" do company = Company.create! valid_attributes put :update, {:id => company.to_param, :company => new_attributes}, valid_session company.reload skip("Add

Why isn't factory_girl operating transactionally for me? - rows remain in database after tests

我与影子孤独终老i 提交于 2019-11-30 11:18:22
问题 I'm trying to use factory_girl to create a "user" factory (with RSpec) however it doesn't seem to be operating transactionally and is apparently failing because of remnant data from previous tests in the test database. Factory.define :user do |user| user.name "Joe Blow" user.email "joe@blow.com" user.password 'password' user.password_confirmation 'password' end @user = Factory.create(:user) Running the first set of tests is fine: spec spec/ ... Finished in 2.758806 seconds 60 examples, 0