我怎样才能让shoda认识到我的多态性关联 [英] How do I get shoulda to recognise my polymorphic association

查看:121
本文介绍了我怎样才能让shoda认识到我的多态性关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前,有人问过一个几乎相同的问题(如何使用匹配器来测试多态关联吗?)尚无确定的答案对我有帮助,因此我正在再次尝试.

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

此操作失败,并显示以下消息

This fails with the following message

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

据我所知,应该通过所有测试,为什么不呢?应该将比对匹配器正常工作".如果进入控制台,我可以轻松创建复杂的治疗方案.有什么想法吗?

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.

(滴答声应该传给彼得·阿尔夫文)

(the tick should got to Peter Alfvin)

这篇关于我怎样才能让shoda认识到我的多态性关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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