Search code examples
rspecruby-on-rails-3.2fixturessti

Getting an Active::Record::StatementInvalid on when running my spec on an STI model


So, here's my issue. I have a Model called AdminNotifications. It is belongs to an AdminUser model. There is a QuorumAdminNotification model which inherits from AdminNotification using STI. It is currently empty. When I run my spec I get this error on all the tests:

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)

If I set the inheritance column to a non-existant column all of the tests pass.

Does anyone have any idea why this is happening?

Here's the relevant code:

app/models/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

spec/models/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

spec/fixtures/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

app/spec/admin_users.yml

admin:
  email: [email protected]
  password: password
  password_confirmation: password

Thanks!


Solution

  • Fixed it.

    The problem was with the type field in the fixtures

    it should be

    QuorumAdminNotification

    not

    Quorum