<false> is not true failure in unit test

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.

On Oct 12, 6:38 am, Jay P. [email protected]
wrote:

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.

Assuming the failure is on the last assertion it’s probably because
you’ve never set family_rate, but your validation asserts that it is
greater than 0.01. You could stick a breakpoint before the test fails
and poke around to see exactly why the object is not valid (or just
print the errors object to the screen or something like that.

Frederick C. wrote:

Assuming the failure is on the last assertion it’s probably because
you’ve never set family_rate, but your validation asserts that it is
greater than 0.01. You could stick a breakpoint before the test fails
and poke around to see exactly why the object is not valid (or just
print the errors object to the screen or something like that.

Thanks fred. Sorry, actually I tried valdating family_rate as well
defining method test_positive_family_rate similar to
test_positive_perpersonrate method. When I tried with family_rate as
well I got two failures. And about sticking the breakpoint I’m not sure
how to do…

Sent from my iPhone

On 12 Oct 2008, at 11:29, Jay P. <rails-mailing-list@andreas-
s.net> wrote:

defining method test_positive_family_rate similar to
test_positive_perpersonrate method. When I tried with family_rate as
well I got two failures. And about sticking the breakpoint I’m not
sure
how to do…

Adding an extra test isn’t going to help. Your test isn’t passing
because your object is not valid

Fred

Frederick C. wrote:

Adding an extra test isn’t going to help. Your test isn’t passing
because your object is not valid

Fred

Where am I wrong? Here’s how I’ve done…

IN 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_per_person_rate
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

def test_positive_family_rate
fee=Fee.new()

fee.family_rate = -1
assert !fee.valid?
assert_equal "should be greater than 0", fee.errors.on(:family_rate)

fee.family_rate = 0
assert !fee.valid?
assert_equal "should be greate than 0", fee.errors.on(:family_rate)

fee.family_rate = 1
assert fee.valid?

end

end

I’m just copying from the book with very (almost no) changes. So, I
can’t figure out why here. thanks

Sent from my iPhone

On 12 Oct 2008, at 12:35, Jay P. <rails-mailing-list@andreas-
s.net> wrote:

Frederick C. wrote:

Adding an extra test isn’t going to help. Your test isn’t passing
because your object is not valid

Fred

Where am I wrong? Here’s how I’ve done…

Print out the object’s errors and you’ll see

Fred

Frederick C. wrote:

Print out the object’s errors and you’ll see

Fred

sorry how do i do that?

Sent from my iPhone

On 12 Oct 2008, at 13:13, Jay P. <rails-mailing-list@andreas-
s.net> wrote:

Frederick C. wrote:

Print out the object’s errors and you’ll see

Fred

sorry how do i do that?

Look at foo.errors

Something like
puts foo.errors.inspect should do the trick.

Or stick
debugger

In an appropriate part of your test. When you hit that line you’ll
drop into the debugger

Frederick C. wrote:

Look at foo.errors

Something like
puts foo.errors.inspect should do the trick.

Or stick
debugger

In an appropriate part of your test. When you hit that line you’ll
drop into the debugger

I put the fee.errors.detect at:

def test_positive_perpersonrate
fee=Fee.new


puts fee.errors.detect
end

but it doesn’t print anything except that already showing error
message…
thanks

As Fred mentioned earlier, you need to set family_rate to
something valid, which accdg to your model validation should
be a number greater than or equal to 0.01.

Otherwise the last assert fee.valid? will return false. You may
have set a valid per_person_rate, but since family_rate is nil,
the object will not be valid.

So looking at your test again, you can do this:

def test_positive_perpersonrate
fee=Fee.new(:family_rate => 1.0) #provide a valid value for
family_rate

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

Hope this helps.

On 13 Oct 2008, at 15:02, Jay P. wrote:

In an appropriate part of your test. When you hit that line you’ll

but it doesn’t print anything except that already showing error
message…

Why would it? I said inspect, not detect. Also you have to put it just
before the failing assertion.

Fred

Frederick C. wrote:

On 13 Oct 2008, at 15:02, Jay P. wrote:

Why would it? I said inspect, not detect. Also you have to put it just
before the failing assertion.

Fred

Sorry, my bad, I wrote incorrectly, but I tried with puts
fee.errors.inspect only. I will try with Franz way… thanks