0

所以,这是我的问题。我有一个名为 AdminNotifications 的模型。它属于 AdminUser 模型。有一个 QuorumAdminNotification 模型,它使用 STI 从 AdminNotification 继承。它目前是空的。当我运行我的规范时,我在所有测试中都得到了这个错误:

Failure/Error: Unable to find matching line from backtrace
     ActiveRecord::StatementInvalid:
       PG::Error: ERROR:  column "admin_user" of relation "admin_notifications" does not exist
       LINE 1: ...admin_notifications" ("type", "event", "message", "admin_use...
                                                                    ^
       : INSERT INTO "admin_notifications" ("type", "event", "message", "admin_user", "created_at", "updated_at", "id") VALUES

('Quorum', 'reached', 'Quorum has been reached', 'admin', '2012-08-28 14:50:43', '2012-08-28 14:50:43', 853808134)

如果我将继承列设置为不存在的列,则所有测试都会通过。

有谁知道为什么会这样?

以下是相关代码:

应用程序/模型/admin_notification.rb

class AdminNotification < ActiveRecord::Base
  belongs_to :admin_user

  attr_accessible :message, :model, :record_id, :type

  scope :addressed, where('admin_user_id IS NOT NULL')
  scope :unaddressed, where('admin_user_id IS NULL')

  def addressed?
    !admin_user.nil?
  end
end

规范/模型/admin_notification_spec.rb

describe AdminNotification do
  fixtures :admin_notifications

  describe "#addressed?" do
    it "should return false if admin_user is not set" do
      AdminNotification.new.should_not be_addressed
    end

    it "should return true when admin_user is set" do
      notification = AdminNotification.new
      notification.stub(:admin_user).and_return(mock(AdminUser, nil?: false))
      notification.should be_addressed
    end
  end

  describe "addressed scope" do
    it "returns all addressed notifications" do
      AdminNotification.addressed.should == admin_notifications(:addressed1, :addressed2)
    end
  end

  describe "unaddressed scope" do
    it "returns all unaddressed notifications" do
      AdminNotification.unaddressed.should == admin_notifications(:unaddressed1, :unaddressed2)
    end
  end
end

规格/夹具/admin_notification.yml

unaddressed1:
  type: Quorum
  event: reached 
  message: "Quorum has been reached"

unaddressed2:
  type: Quorum
  event: test 
  message: "Test message"

addressed1:
  type: Quorum
  event: reached 
  message: "Quorum has been reached"
  admin_user: admin

addressed2:
  type: Quorum
  event: reached 
  message: "Quorum has been reached"
  admin_user: admin

应用程序/规范/admin_users.yml

admin:
  email: admin@example.com
  password: password
  password_confirmation: password

谢谢!

4

1 回答 1

0

修复。

问题在于固定装置中的类型字段

它应该是

QuorumAdminNotification

不是

法定人数

于 2012-08-28T15:32:55.323 回答