Cucumber duplicate class problem: AssociationTypeMismatch

我的未来我决定 提交于 2019-12-24 06:39:05

问题


I am having a problem with Cucumber when there is a reference to self in a model callback method.

Code example below. The error is like: Person(#26738680) expected, got Person(#29003170) (ActiveRecord::AssociationTypeMismatch)

class Person < ActiveRecord::Base


  has_one :person_profile    
  accepts_nested_attributes_for : person_profile                             
  after_create :new_person_profile

  private 

  def new_person_profile
    person_profile = PersonProfile.new( 
      ...,
      :person => self  # <--- this causes the error in cucumber
    ) 
    self.person_profile.save
  end

The failing scenario is like:

  Scenario: Student logs in for the first time
    Given I am a valid Student

And the failing steps:

Before do
  include Authlogic::TestCase
  activate_authlogic
end

def valid_person
    @current_person = Factory.create(:valid_person, :person_profile => new_person_profile('Kelly','Hope'))
end  

Given /^I am a valid Student$/ do
  valid_student
end

Thank you Adam


回答1:


This doesn't directly answer your question, but I'm going to suggest that the after_create callback is unnecessary. You've already defined accepts_nested_attributes_for in the Person model -- if the objective is to create a Person and an associated PersonProfile in one step, you should be able to do (eg.):

params = { :person => { :name => "John Smith",  
 :person_profile_attributes => { :some_profile_attribute => "some_value" }}}
@person = Person.create!(params[:person])


来源:https://stackoverflow.com/questions/5073485/cucumber-duplicate-class-problem-associationtypemismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!