【问题标题】:How do I get shoulda to recognise my polymorphic association如何让我应该识别我的多态关联
【发布时间】:2013-08-23 14:16:12
【问题描述】:

之前有人问过一个几乎相同的问题 (How to use shoulda matchers to test a polymorphic assoication?),但没有明确的答案可以帮助我,所以我再试一次。

我正在使用 shoulda 测试我的关联,但以下测试失败

require 'spec_helper'

describe LabourEpidural do
  before {@treatment = FactoryGirl.build :treatment}
  subject {@treatment}
  it{should have_many :complications}
end

失败并显示以下消息

Failure/Error: it{should have_many :complications}
   Expected Treatment to have a has_many association called complications (Complication does not have a complicatable_id foreign key.)

问题是我的 Complication 表确实有一个complicatable_id 列。以下是我的模型的相关部分;

class Treatment < ActiveRecord::Base
  has_many :complications, as: :complicatable, dependent: :destroy
end

class Complication < ActiveRecord::Base
  belongs_to :complicatable, polymorphic: true
end

来自我的 schema.rb;

create_table "complications", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "complicatable_id"
  t.string   "complicatable_type"
end

据我所知,一切都已准备就绪,可以通过 shoulda 测试,那为什么不呢?应该匹配器应该与多态关联“正常工作”。如果我进入控制台,我可以轻松地创建具有并发症的治疗方法。有什么想法吗?

【问题讨论】:

  • 您是否尝试从控制台访问关联,它们是否有效?
  • 您是否在测试数据库上运行过迁移?
  • 是的,我已经在控制台中尝试过关联,是的,它们确实有效。
  • 呃,不,我没有在我的测试数据库中运行迁移。哎呀。现在一切都好。把它写成答案,你会得到一个漂亮的闪亮绿色勾号。 (羞愧地低着头走开)。

标签: ruby-on-rails rspec polymorphic-associations shoulda


【解决方案1】:

在您的测试数据库上运行迁移!

...我被多次发现的错误。

(勾号应该是 Peter Alfvin)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2011-03-19
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多