I am having a strange problem with FactoryGirl, Spork and Cucumber. It seems to be happening when on a ''before_validation'' hook another model object is created and saved. Below is a cut down version of the model in question. When it is created via FactoryGirl, I get this error: Country(#28734490) expected, got Country(#28417320) (ActiveRecord::AssociationTypeMismatch) class TuteonParent < ActiveRecord::Base has_one :address, :as => :addressable has_one :fs_parent_profile accepts_nested_attributes_for :fs_parent_profile, :address validates_presence_of :address validates_associated :address validates_presence_of :core_profile validates_associated :core_profile validates_presence_of :fs_parent_profile validates_associated :fs_parent_profile before_validation :create_fs_parent_profile def create_fs_parent_profile fs_parent_profile = FsParentProfile.new( :state => address.country.name, :curriculum_state => address.country.name, :country => address.country, :fs_curriculum_country => FsCurriculumCountry.find_by_country_id(address.country.id) ) self.fs_parent_profile = fs_parent_profile end ... and the simplified Factory definition is as follows: Factory.define :valid_parent, :class => Parent do |p| tp.address { Factory.create(:address) } end If anyone can shed some light on this would be much appreciated! Thank you Adam -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I forgot to add - this is only an issue with Spork and Cucumber. Rspec and Spork run fine, and before I got Spork working (I think) this was working with Cucumber. I think it *may* have something to do with where I am loading the Factories or "require ''factory_girl''" I tried to copy the layout from the RSpec spec_helper.rb. This is how my features/support/env.rb looks like: require ''rubygems'' require ''spork'' Spork.prefork do ENV["RAILS_ENV"] ||= "test" require File.expand_path(File.dirname(__FILE__) + ''/../../config/ environment'') require ''cucumber/formatter/unicode'' # Remove this line if you don''t want Cucumber Unicode support require ''cucumber/rails/world'' require ''cucumber/rails/active_record'' require ''cucumber/web/tableish'' require ''capybara/rails'' require ''capybara/cucumber'' require ''capybara/session'' require ''factory_girl'' Capybara.default_selector = :css ActionController::Base.allow_rescue = false Cucumber::Rails::World.use_transactional_fixtures = true if defined?(ActiveRecord::Base) begin require ''database_cleaner'' DatabaseCleaner.strategy = :truncation rescue LoadError => ignore_if_database_cleaner_not_present end end end Spork.each_run do require ''factory_girl_rails'' Dir.glob(File.join(File.dirname(__FILE__), ''../../spec/factories/ *.rb'')).each {|f| require f } end On Jan 25, 2:42 am, adam <adam.elemen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am having a strange problem with FactoryGirl, Spork and Cucumber. > It seems to be happening when on a ''before_validation'' hook another > model object is created and saved. > > Below is a cut down version of the model in question. When it is > created via FactoryGirl, I get this error: > > Country(#28734490) expected, got Country(#28417320) > (ActiveRecord::AssociationTypeMismatch) > > class TuteonParent < ActiveRecord::Base > > has_one :address, :as => :addressable > has_one :fs_parent_profile > > accepts_nested_attributes_for :fs_parent_profile, :address > > validates_presence_of :address > validates_associated :address > > validates_presence_of :core_profile > validates_associated :core_profile > > validates_presence_of :fs_parent_profile > validates_associated :fs_parent_profile > > before_validation :create_fs_parent_profile > > def create_fs_parent_profile > > fs_parent_profile = FsParentProfile.new( > :state => address.country.name, > :curriculum_state => address.country.name, > :country => address.country, > :fs_curriculum_country => > FsCurriculumCountry.find_by_country_id(address.country.id) > ) > > self.fs_parent_profile = fs_parent_profile > end > > ... and the simplified Factory definition is as follows: > > Factory.define :valid_parent, :class => Parent do |p| > > tp.address { Factory.create(:address) } > > end > > If anyone can shed some light on this would be much appreciated! > Thank you > > Adam-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
OK - solved this issue finally. I used ''find'' instead of the association: def create_fs_parent_profile self.fs_parent_profile = FsParentProfile.new( :state => address.country.name, :curriculum_state => address.country.name, :country => Country.find(address.country.id), :fs_curriculum_country => FsCurriculumCountry.find_by_country_id(address.country.id) ) On Jan 25, 3:23 am, adam <adam.elemen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I forgot to add - > > this is only an issue with Spork and Cucumber. Rspec and Spork run > fine, and before I got Spork working (I think) this was working with > Cucumber. > I think it *may* have something to do with where I am loading the > Factories or "require ''factory_girl''" > > I tried to copy the layout from the RSpec spec_helper.rb. This is how > my features/support/env.rb looks like: > > require ''rubygems'' > require ''spork'' > > Spork.prefork do > > ENV["RAILS_ENV"] ||= "test" > require File.expand_path(File.dirname(__FILE__) + ''/../../config/ > environment'') > > require ''cucumber/formatter/unicode'' # Remove this line if you don''t > want Cucumber Unicode support > require ''cucumber/rails/world'' > require ''cucumber/rails/active_record'' > require ''cucumber/web/tableish'' > > require ''capybara/rails'' > require ''capybara/cucumber'' > require ''capybara/session'' > > require ''factory_girl'' > > Capybara.default_selector = :css > > ActionController::Base.allow_rescue = false > Cucumber::Rails::World.use_transactional_fixtures = true > if defined?(ActiveRecord::Base) > begin > require ''database_cleaner'' > DatabaseCleaner.strategy = :truncation > rescue LoadError => ignore_if_database_cleaner_not_present > end > end > > end > > Spork.each_run do > require ''factory_girl_rails'' > Dir.glob(File.join(File.dirname(__FILE__), ''../../spec/factories/ > *.rb'')).each {|f| require f } > end > > On Jan 25, 2:42 am, adam <adam.elemen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am having a strange problem with FactoryGirl, Spork and Cucumber. > > It seems to be happening when on a ''before_validation'' hook another > > model object is created and saved. > > > Below is a cut down version of the model in question. When it is > > created via FactoryGirl, I get this error: > > > Country(#28734490) expected, got Country(#28417320) > > (ActiveRecord::AssociationTypeMismatch) > > > class TuteonParent < ActiveRecord::Base > > > has_one :address, :as => :addressable > > has_one :fs_parent_profile > > > accepts_nested_attributes_for :fs_parent_profile, :address > > > validates_presence_of :address > > validates_associated :address > > > validates_presence_of :core_profile > > validates_associated :core_profile > > > validates_presence_of :fs_parent_profile > > validates_associated :fs_parent_profile > > > before_validation :create_fs_parent_profile > > > def create_fs_parent_profile > > > fs_parent_profile = FsParentProfile.new( > > :state => address.country.name, > > :curriculum_state => address.country.name, > > :country => address.country, > > :fs_curriculum_country => > > FsCurriculumCountry.find_by_country_id(address.country.id) > > ) > > > self.fs_parent_profile = fs_parent_profile > > end > > > ... and the simplified Factory definition is as follows: > > > Factory.define :valid_parent, :class => Parent do |p| > > > tp.address { Factory.create(:address) } > > > end > > > If anyone can shed some light on this would be much appreciated! > > Thank you > > > Adam-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
well, I kinda feel like I am talking to myself here... but although solving this issue in one part of my app tests, it has come up in another: I have a couple of Factories defined - Factory.define :valid_student, :class => Student do |s| s.email { "#{Factory.next(:name)}@example.com" } s.gender ''female'' s.date_of_birth { Factory.next(:dob) } s.current_school { Factory.next(:school) } s.current_school_year 10 s.core_profile { Factory.create(:student_profile) } # <---- this is causing an error s.parent { Factory.create(:valid_parent, :tc_check => "1") } end Factory.define :student_profile, :class => CoreProfile do |cp| cp.first_name ''Kelly'' cp.last_name ''Hope'' cp.user_name { Factory.next(:user_name) } cp.password ''password'' cp.password_confirmation ''password'' end class Student < ActiveRecord::Base has_one :core_profile, :as => :core_profileable accepts_nested_attributes_for :core_profile after_initialize :build_associations def build_associations build_core_profile unless core_profile end end The error is similar: CoreProfile(#26997920) expected, got CoreProfile(#29695630) (ActiveRecord::AssociationTypeMismatch) Any tips on this would be received with much gratitude! Thanks Adam On Jan 25, 1:36 pm, adam <adam.elemen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> OK - solved this issue finally. > > I used ''find'' instead of the association: > > def create_fs_parent_profile > self.fs_parent_profile = FsParentProfile.new( > :state => address.country.name, > :curriculum_state => address.country.name, > :country => Country.find(address.country.id), > :fs_curriculum_country => > FsCurriculumCountry.find_by_country_id(address.country.id) > ) > > On Jan 25, 3:23 am, adam <adam.elemen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I forgot to add - > > > this is only an issue with Spork and Cucumber. Rspec and Spork run > > fine, and before I got Spork working (I think) this was working with > > Cucumber. > > I think it *may* have something to do with where I am loading the > > Factories or "require ''factory_girl''" > > > I tried to copy the layout from the RSpec spec_helper.rb. This is how > > my features/support/env.rb looks like: > > > require ''rubygems'' > > require ''spork'' > > > Spork.prefork do > > > ENV["RAILS_ENV"] ||= "test" > > require File.expand_path(File.dirname(__FILE__) + ''/../../config/ > > environment'') > > > require ''cucumber/formatter/unicode'' # Remove this line if you don''t > > want Cucumber Unicode support > > require ''cucumber/rails/world'' > > require ''cucumber/rails/active_record'' > > require ''cucumber/web/tableish'' > > > require ''capybara/rails'' > > require ''capybara/cucumber'' > > require ''capybara/session'' > > > require ''factory_girl'' > > > Capybara.default_selector = :css > > > ActionController::Base.allow_rescue = false > > Cucumber::Rails::World.use_transactional_fixtures = true > > if defined?(ActiveRecord::Base) > > begin > > require ''database_cleaner'' > > DatabaseCleaner.strategy = :truncation > > rescue LoadError => ignore_if_database_cleaner_not_present > > end > > end > > > end > > > Spork.each_run do > > require ''factory_girl_rails'' > > Dir.glob(File.join(File.dirname(__FILE__), ''../../spec/factories/ > > *.rb'')).each {|f| require f } > > end > > > On Jan 25, 2:42 am, adam <adam.elemen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I am having a strange problem with FactoryGirl, Spork and Cucumber. > > > It seems to be happening when on a ''before_validation'' hook another > > > model object is created and saved. > > > > Below is a cut down version of the model in question. When it is > > > created via FactoryGirl, I get this error: > > > > Country(#28734490) expected, got Country(#28417320) > > > (ActiveRecord::AssociationTypeMismatch) > > > > class TuteonParent < ActiveRecord::Base > > > > has_one :address, :as => :addressable > > > has_one :fs_parent_profile > > > > accepts_nested_attributes_for :fs_parent_profile, :address > > > > validates_presence_of :address > > > validates_associated :address > > > > validates_presence_of :core_profile > > > validates_associated :core_profile > > > > validates_presence_of :fs_parent_profile > > > validates_associated :fs_parent_profile > > > > before_validation :create_fs_parent_profile > > > > def create_fs_parent_profile > > > > fs_parent_profile = FsParentProfile.new( > > > :state => address.country.name, > > > :curriculum_state => address.country.name, > > > :country => address.country, > > > :fs_curriculum_country => > > > FsCurriculumCountry.find_by_country_id(address.country.id) > > > ) > > > > self.fs_parent_profile = fs_parent_profile > > > end > > > > ... and the simplified Factory definition is as follows: > > > > Factory.define :valid_parent, :class => Parent do |p| > > > > tp.address { Factory.create(:address) } > > > > end > > > > If anyone can shed some light on this would be much appreciated! > > > Thank you > > > > Adam-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jeff Bigler
2011-Feb-22 10:01 UTC
Re: Factory Girl, Spork and Cucumber - duplicate model classes
I''m experiencing the exact same problem Adam. I haven''t been able to figure out what is causing it either... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.