factory-bot

Factory Girl with polymorphic association for has_many and has_one

本小妞迷上赌 提交于 2019-12-03 20:32:23
I am currently working on a project and I wanted to create tests using factory girl but I'm unable to make it work with polymorphic has_many association. I've tried many different possibilities mentioned in other articles but it still doesn't work. My model looks like this: class Restaurant < ActiveRecord::Base has_one :address, as: :addressable, dependent: :destroy has_many :contacts, as: :contactable, dependent: :destroy accepts_nested_attributes_for :contacts, allow_destroy: true accepts_nested_attributes_for :address, allow_destroy: true validates :name, presence: true #validates :address,

How to include a module in a factory_girl factory?

家住魔仙堡 提交于 2019-12-03 17:06:49
问题 I'm trying to reuse a helper method in all my factories, however I cannot get it to work. Here's my setup: Helper module (in spec/support/test_helpers.rb) module Tests module Helpers # not guaranteed to be unique, useful for generating passwords def random_string(length = 20) chars = ['A'..'Z', 'a'..'z', '0'..'9'].map{|r|r.to_a}.flatten (0...length).map{ chars[rand(chars.size)] }.join end end end A factory (in spec/factories/users.rb) FactoryGirl.define do factory :user do sequence(:username)

factory girl multiple has_many through's

时间秒杀一切 提交于 2019-12-03 14:34:06
I need to create some factories that are made of multiple has many through's Here are my models Topic has_many :plan_topics has_many :plans, :through => :plan_topics PlanTopic belongs_to :plan belongs_to :topic Plan has_many :subscriptions has_many :members, :through => :subscriptions has_many :plan_topics has_many :topics, :through => :plan_topics Subscription belongs_to :member belongs_to :plan Member has_many :subscriptions has_many :plans, :through => :subscriptions Here is what I have Factory.define :topic do |topic| topic.name "Operations" end Factory.define :plan do |plan| plan.title "A

Factory already registered: user (FactoryGirl::DuplicateDefinitionError)

吃可爱长大的小学妹 提交于 2019-12-03 14:23:17
问题 Description of problem: - I've setup factory_girl_rails however whenever I try and load a factory it's trying to load it multiple times. Environment: - rails (3.2.1) - factory_girl (2.5.2) - factory_girl_rails (1.6.0) - ruby-1.9.3-p0 [ x86_64 ] > rake spec --trace ** Execute environment -- Creating User Factory -- Creating User Factory rake aborted! Factory already registered: user The only other thing I've changed is: /config/initializers/generator.rb Rails.application.config.generators do

RSpec-rails-capybara - different failures with :js => true and without

。_饼干妹妹 提交于 2019-12-03 12:42:46
问题 I'm building a setup screen for billing of individuals. The controller/views are in the Admin namespace. When run the first test without :js => true I get one failure, which I assume is down to the fact that the link does not work as its a helper which uses a js script to build a nested set of fields (Based on Railscasts Single form, multiple tables - Nested Attributes). Failures: 1) Patient Setup create patient bill heading - with extended details -with valid data Failure/Error: fill_in

railstutorial.org - undefined method `Factory'

会有一股神秘感。 提交于 2019-12-03 12:41:47
I'm attempting to follow railstutorial.org, and am currently on Chapter 7, where you start using factories: http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories I'm using Rails 3.0.1 and ruby-1.9.2-p0 I can't for the life of me get my rspec tests to pass though, the error i get is Failures: 1) UsersController GET 'show' should be successful Failure/Error: @user = Factory(:user) undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x00000101cc5608> # ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top

How come Factory Girl isn't sequencing unique attributes?

 ̄綄美尐妖づ 提交于 2019-12-03 12:10:54
问题 My controller spec fails because Factory Girl seems to be creating non-unique Users even though I sequence the User attributes that need to be unique. The Errors 1) TopicsController POST #create when topic is invalid should render new Failure/Error: let(:invalid_topic) {Factory.build :invalid_topic} ActiveRecord::RecordInvalid:Validation failed: Email has already been taken, Username has already been taken 2) TopicsController POST #create when topic is valid should redirect to show Failure

Factory with carrierwave upload field [duplicate]

落爺英雄遲暮 提交于 2019-12-03 11:14:55
问题 This question already has answers here : How Do I Use Factory Girl To Generate A Paperclip Attachment? (8 answers) Closed 4 years ago . Hello i need to build up Factory for my model, for example Factory.define :farm do |f| f.name { Factory.next :name } f.harvest '3' f.offers 'Random' f.latitude '43' f.longitude '-70' f.about 'We rocks!' f.logo { Factory.next :logo } # this doesn't work end For now im just pass string "#{n}.jpg" into my logo field and this dont work, how to evalute this field?

AssociationTypeMismatch and FactoryGirl

老子叫甜甜 提交于 2019-12-03 11:13:04
问题 This has been causing some frustration recently... It seems that using Factories in my cucumber tests, in some situations causes AssociationTypeMismatch errors such as: MyModel(#65776650) expected, got MyModel(#28190030) (ActiveRecord::AssociationTypeMismatch) These seem to happen when there is an association reference - as if the Factory created object is different to the real one. See this question for more details: Cucumber duplicate class problem: AssociationTypeMismatch I have been

Why do I get an undefined method 'have' error when running Rspec?

删除回忆录丶 提交于 2019-12-03 11:03:01
问题 I recently upgraded to Rails 4 and everything works fine except for my Rspec tests. require 'spec_helper' describe Invoice do before :each do @user = FactoryGirl.create(:activated_user) person = FactoryGirl.create(:person, :user => @user, :company => nil) @project = FactoryGirl.create(:project, :user => @user, :person_ids => [person.id], :invoice_recipient_id => person.id) end it "has a valid factory" do expect(FactoryGirl.build(:invoice, :project => @project, :user => @user)).to be_valid end