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`
存在しないテーブルのレコードを削除しようとせずにテストを実行するにはどうすればよいですか?