我如何让应该承认我的多态关联 [英] How do I get shoulda to recognise my polymorphic association

查看:22
本文介绍了我如何让应该承认我的多态关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前已经问过一个几乎相同的问题(如何使用 shoulda 匹配器来测试多态关联?)但是有没有明确的答案对我有帮助,所以我再试一次.

An almost indentical question to this has been asked before (How to use shoulda matchers to test a polymorphic assoication?) but there was no definitive answer that helps me, so I am trying again.

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

I am using shoulda to test my associations and the following test fails

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 列.这是我的模型的相关部分;

The problem is that my Complication table does have a complicatable_id column. Here are the relevant parts of my models;

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

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

来自我的 schema.rb;

and from my 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

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

As far as I can tell everything is in place for the shoulda test to pass, so why isn't it? Shoulda matchers are supposed to 'just work' with polymorphic associations. If I go into the console I can easily create treatments with complications. Any ideas?

推荐答案

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

Run migrations on your test database!

...一个我被多次发现的错误.

...a mistake I have been caught by many times.

(应该是 Peter Alfvin 打勾)

(the tick should got to Peter Alfvin)

这篇关于我如何让应该承认我的多态关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆