Forum: Ruby on Rails <false> is not true failure in unit test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-12 07:38
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]:
<false> 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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-12 12:05
(Received via mailing list)
On Oct 12, 6:38 am, Jay Pangmi <rails-mailing-l...@andreas-s.net>
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.
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-12 12:29
Frederick Cheung 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..
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-12 13:15
(Received via mailing list)
Sent from my iPhone

On 12 Oct 2008, at 11:29, Jay Pangmi <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
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-12 13:35
Frederick Cheung 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-12 14:06
(Received via mailing list)
Sent from my iPhone

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

>
> Frederick Cheung 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
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-12 14:13
Frederick Cheung wrote:
> Print out the object's errors and you'll see
>
> Fred

sorry how do i do that?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-12 15:42
(Received via mailing list)
Sent from my iPhone

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

>
> Frederick Cheung 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
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-13 16:02
Frederick Cheung 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
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2008-10-13 16:12
(Received via mailing list)
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-13 16:21
(Received via mailing list)
On 13 Oct 2008, at 15:02, Jay Pangmi 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
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-13 17:04
Frederick Cheung wrote:
> On 13 Oct 2008, at 15:02, Jay Pangmi 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
This topic is locked and can not be replied to.