Cucumber重复类问题:AssociationTypeMismatch

发布于 2024-10-18 15:07:37 字数 990 浏览 0 评论 0原文

当模型回调方法中引用 self 时,我遇到了 Cucumber 问题。

下面的代码示例。 错误是这样的: 预计 Person(#26738680),得到 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

失败的场景如下:

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

失败的步骤:

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

谢谢 亚当

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温馨耳语 2024-10-25 15:07:37

这并不能直接回答你的问题,但我建议 after_create 回调是不必要的。您已经在 Person 模型中定义了 accepts_nested_attributes_for - 如果目标是创建一个 Person 和关联的 PersonProfile一步,你应该能够做到(例如):

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

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])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文