我有一个基于 ActiveModel 的类(基于这个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
还有一个单元测试:
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
当我去运行测试时,它说:
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`
如何在不尝试删除不存在表中的记录的情况下运行测试?