Hi, I’ve done as follows:
MODEL:
class Fee < ActiveRecord::Base
has_many :campsites
validates_presence_of :per_person_rate, :family_rate
validates_numericality_of :per_person_rate, :family_rate
def validate
errors.add(:per_person_rate, “should be greater than 0”) if
per_person_rate.nil? ||
per_person_rate
< 0.01
errors.add(:family_rate, “should be greater than 0”) if
family_rate.nil? ||
family_rate
< 0.01
end
end
IN fee_test.rb
require File.dirname(FILE) + ‘/…/test_helper’
class FeeTest < ActiveSupport::TestCase
Replace this with your real tests.
def test_truth
assert true
end
def test_invalid_with_empty_attribute
fee=Fee.new
assert !fee.valid?
assert fee.errors.invalid?(:per_person_rate)
assert fee.errors.invalid?(:family_rate)
end
def test_positive_perpersonrate
fee=Fee.new()
fee.per_person_rate = -1.0
assert !fee.valid?
assert_equal "should be greater than 0",
fee.errors.on(:per_person_rate)
fee.per_person_rate = 0.0
assert !fee.valid?
assert_equal "should be greater than 0",
fee.errors.on(:per_person_rate)
fee.per_person_rate = 1.0
assert fee.valid?
end
end
when I run: ruby test/unit/fee_test.rb I get:
1)Failure:
test_positive_perpersonrate(FeeTest)
[C:/Aptana_Studio_Setup_Windows/aptana/plugins/org.jruby_1.1.0.5965_RC2p2/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/testing/default.rb:7]:
is not true
3 tests, 9 assertions, 1 failure, 0 errors
I tried as fee.per_person_rate = 0 as well but it was giving me the same
failure so I put 0.0 as I have declared it as float but still getting
the same failure. So, can someone please let me know why coz this is my
first time doing unit testing. Thanks.