barnaclebarnes
barnaclebarnes

Reputation: 366

How do I unit test non-database backed ActiveModel models?

I have an ActiveModel based class (based on this RailsCast):

class ShareTransactionWizard
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :num_issue, :num_for, :type

  validates_presence_of :num_issue, :num_for, :type

  validate :issue_greater_than_for,  if: Proc.new {a | a.type == 'split' }

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

  def issue_greater_than_for
   if !(num_issue > num_for)
      errors.add(:num_issue, "Split must have issued greater than for")
    end
  end
end

And a unit test:

require 'test_helper'

class ShareTransactionWizardTest < ActiveSupport::TestCase
  test "Must have :issue greater than :for if type is 'split'" do
    stw = ShareTransactionWizard.new(num_issue: 1, num_for: 10, type: 'split')
    assert !stw.valid?
  end    
end

When I go to run the test it says:

Error:  
test_Must_have_:issue_greater_than_:for_if_type_is_'split'(ShareTransactionWizardTest)
ActiveRecord::StatementInvalid: Mysql2::Error: Table
'companybox_test.share_transaction_wizards' doesn't exist: DELETE FROM    
`share_transaction_wizards`

How to I get the test to run without trying to delete the records in the non-existent table?

Upvotes: 0

Views: 107

Answers (1)

barnaclebarnes
barnaclebarnes

Reputation: 366

Looks like it was because when I created the model it put some fixtures in the fixtures folder. Deleting those fixtures made the error go away.

Upvotes: 2

Related Questions