It’s taken a little too long to see the light for unit testing, but the
time has come! I realize that this isn’t the most exciting subject for
most developers, but hopefully a couple of you have some sage advice.
I’ve been trying out some test cases, and have focused on a single model
trying to figure it all out before moving to the next one.
The table / model I am working with is log_edits:
id (int)
employee_id (int)
edit_time (datetime)
edit_position (varchar 10)
original_value (float 8)
new_value (float 8)
source_table (varchar 50)
My model file log_edit.rb:
class LogEdit < ActiveRecord::Base
validates_presence_of :employee_id, :edit_time, :edit_position
validates_presence_of :original_value, :new_value :source_table
validates_numericality_of :employee_id, :original_value, :new_value
end
Is there a way to verify if the following is true within the model?
employee_id is an integer
orig_value and new_value are either whole or half numbers eg 12.0 or 3.5
and they are less then 50 but >= 0?
(a reg ex for .0 or .5 might work)
I’ve written up a small set of test cases to check it out, but its not
working quite how I expected. Here is a snipped of what is failing.
require File.dirname(FILE) + ‘/…/test_helper’
require ‘test/unit’
require ‘log_edit’
class LogEditTest < Test::Unit::TestCase
fixtures :log_edits
def test_invalid_with_empty_attributes
edits_time = LogEdit.new
assert !edits_time.valid?
# value not set. should be invalid and therefore pass
assert edits_time.errors.invalid?(:employee_id)
# Set the value and it should fail since it is valid,
# but no failure appears when run!!
edits_time.employee_id = 123
assert edits_time.errors.invalid?(:employee_id)
end
end