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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Oct 12, 6:38 am, Jay Pangmi <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sent from my iPhone On 12 Oct 2008, at 11:29, Jay Pangmi <rails-mailing-list@andreas- s.net> wrote:> > 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.. >Adding an extra test isn''t going to help. Your test isn''t passing because your object is not valid Fred> -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> Adding an extra test isn''t going to help. Your test isn''t passing > because your object is not valid > > FredWhere 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> 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 > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> Print out the object''s errors and you''ll see > > Fredsorry how do i do that? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 debuggerI 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 13 Oct 2008, at 15:02, Jay Pangmi wrote:> > 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..Why would it? I said inspect, not detect. Also you have to put it just before the failing assertion. Fred> > thanks > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. > > FredSorry, my bad, I wrote incorrectly, but I tried with puts fee.errors.inspect only. I will try with Franz way.. thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---