Sterling Anderson
2006-Dec-04 18:47 UTC
Unit testing validates_associated and HABTM relationships
I''m trying to set up some unit tests and they are failing on just basic tests on one model. The failure is: 1) Failure: test_invalid_with_empty_attributes(WorkAreaTaskTest) [test/unit/work_area_task_test.rb:15]: <false> is not true. My model is: class WorkAreaTask < ActiveRecord::Base belongs_to :task belongs_to :work_area has_many :employee_tasks, :dependent => :destroy acts_as_list :scope => :work_area validates_presence_of :task_id validates_presence_of :work_area_id validates_presence_of :task validates_associated :task end And my unit test is: class WorkAreaTaskTest < Test::Unit::TestCase fixtures :work_area_tasks, :employee_type_tasks, :tasks, :work_areas # Replace this with your real tests. def test_truth assert true end def test_invalid_with_empty_attributes work_area_task = WorkAreaTask.new assert !work_area_task.valid? assert work_area_task.errors.invalid?(:task_id) assert work_area_task.errors.invalid?(:work_area_id) assert work_area_task.errors.invalid?(:task) end end The failure occurs at the assert invalid tests. -- Sterling Anderson sterling [at] sterlinganderson.net http://sterlinganderson.net/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Dec-04 18:56 UTC
Re: Unit testing validates_associated and HABTM relationships
Can''t tell from your posting which line is line 15. Where exactly is it failing? On 12/4/06, Sterling Anderson <sterling-m5t3XAY/qu5DjbiTPaivd+TW4wlIGRCZ@public.gmane.org> wrote:> > > I''m trying to set up some unit tests and they are failing on just > basic tests on one model. > > The failure is: > > 1) Failure: > test_invalid_with_empty_attributes(WorkAreaTaskTest) > [test/unit/work_area_task_test.rb:15]: > <false> is not true. >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sterling Anderson
2006-Dec-04 19:00 UTC
Re: Unit testing validates_associated and HABTM relationships
It''s failing on this line: assert work_area_task.errors.invalid?(:task_id) The only way to get it to not fail is to comment that line and the next 2 as well. So it''s like is getting the the IDs and the task from somewhere, because they should be empty. On 12/4/06, Bala Paranj <bcparanj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can''t tell from your posting which line is line 15. Where exactly is it > failing? > > > On 12/4/06, Sterling Anderson <sterling-m5t3XAY/qu5DjbiTPaivd+TW4wlIGRCZ@public.gmane.org> wrote: > > > > I''m trying to set up some unit tests and they are failing on just > > basic tests on one model. > > > > The failure is: > > > > 1) Failure: > > test_invalid_with_empty_attributes(WorkAreaTaskTest) > > [test/unit/work_area_task_test.rb:15]: > > <false> is not true. > > > > > >-- Sterling Anderson sterling [at] sterlinganderson.net http://sterlinganderson.net/ 608.239.8387 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Dec-04 19:31 UTC
Re: Unit testing validates_associated and HABTM relationships
I don''t know why you are checking the id''s. The id''s are usually keys that are generated automatically when you save the model. Is there any reason why you are validating id''s? If you cannot save the model, the id fields will be blank. On 12/4/06, Sterling Anderson <sterling-m5t3XAY/qu5DjbiTPaivd+TW4wlIGRCZ@public.gmane.org> wrote:> > > It''s failing on this line: > > assert work_area_task.errors.invalid?(:task_id) > > The only way to get it to not fail is to comment that line and the > next 2 as well. So it''s like is getting the the IDs and the task from > somewhere, because they should be empty. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sterling Anderson
2006-Dec-04 20:27 UTC
Re: Unit testing validates_associated and HABTM relationships
I''m not checking the id''s from the model I am testing. I am checking the foreign key id''s, ie. the id''s from the related tables that need to be there for the model to be valid. That''s why I am checking for them. On 12/4/06, Bala Paranj <bcparanj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I don''t know why you are checking the id''s. The id''s are usually keys that > are generated automatically when you save the model. Is there any reason why > you are validating id''s? > > If you cannot save the model, the id fields will be blank. > > > On 12/4/06, Sterling Anderson <sterling-m5t3XAY/qu5DjbiTPaivd+TW4wlIGRCZ@public.gmane.org> wrote: > > > > It''s failing on this line: > > > > assert work_area_task.errors.invalid?(:task_id) > > > > The only way to get it to not fail is to comment that line and the > > next 2 as well. So it''s like is getting the the IDs and the task from > > somewhere, because they should be empty. > > > > > > > > > > > >-- Sterling Anderson sterling [at] sterlinganderson.net http://sterlinganderson.net/ 608.239.8387 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Dec-04 21:21 UTC
Re: Unit testing validates_associated and HABTM relationships
Read this first: http://manuals.rubyonrails.com/read/chapter/27 Test the behavior, not the implementation. You don''t have to check the foreign keys. You can save a valid record and read it back and compare it with fixture data. It is very similar to the example from the above tutorial. Remove validates_presence_of :task_id, :work_area_id from your model class. I would love to be proved wrong here. On 12/4/06, Sterling Anderson <sterling-m5t3XAY/qu5DjbiTPaivd+TW4wlIGRCZ@public.gmane.org> wrote:> > > I''m not checking the id''s from the model I am testing. I am checking > the foreign key id''s, ie. the id''s from the related tables that need > to be there for the model to be valid. That''s why I am checking for > them. >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sterling Anderson
2006-Dec-04 21:47 UTC
Re: Unit testing validates_associated and HABTM relationships
Ahhh. I see what you are saying. Rooky mistake on my part. :-) So basically the belongs_to is going to take care of the foreign keys, so there''s no reason for me to have "validates_X_of" for them is what you are saying? On 12/4/06, Bala Paranj <bcparanj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Read this first: > http://manuals.rubyonrails.com/read/chapter/27 > > Test the behavior, not the implementation. You don''t have to check the > foreign keys. You can save a valid record and read it back and compare it > with fixture data. It is very similar to the example from the above > tutorial. Remove validates_presence_of :task_id, :work_area_id from your > model class. I would love to be proved wrong here. > > On 12/4/06, Sterling Anderson <sterling-m5t3XAY/qu5DjbiTPaivd+TW4wlIGRCZ@public.gmane.org> wrote: > > > > I''m not checking the id''s from the model I am testing. I am checking > > the foreign key id''s, ie. the id''s from the related tables that need > > to be there for the model to be valid. That''s why I am checking for > > them. > >-- Sterling Anderson sterling [at] sterlinganderson.net http://sterlinganderson.net/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Dec-04 22:00 UTC
Re: Unit testing validates_associated and HABTM relationships
Yes. Also before you start writing tests, jot down specifications in a sheet of paper. Then write : test_should_your_written_specification_one, two etc. When you do TDD really well you are actually thinking in terms of behavior. If you really want to push the envelope and have some time to kill, then check out rSpec. There is a nice Google video that was presented at Google about rSpec. For your viewing pleasure: http://video.google.com/videoplay?docid=8135690990081075324&q=bdd&hl=en On 12/4/06, Sterling Anderson <sterling-m5t3XAY/qu5DjbiTPaivd+TW4wlIGRCZ@public.gmane.org> wrote:> > > Ahhh. I see what you are saying. Rooky mistake on my part. :-) > > So basically the belongs_to is going to take care of the foreign keys, > so there''s no reason for me to have "validates_X_of" for them is what > you are saying? > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---