RichardOnRails
2008-Jul-09 15:30 UTC
Simple app works fine but unit tests don''t work as intended
I have a simple rails 2.0.2 app with one model, Portfolio, and generated CRUD. I''ve added validation to the model and tested it thoroughly by hand -- it works fine. I tried to create a unit test to ensure no inadvertent future changes break my validations. The relevant code is at http://www.pastie.org/230714. Question 1: ruby test/unit/portfolio_test.rb fails with (essentially) portfolio_test_rb:13 is not true. Why did this fail Question 2: I thought perhaps the previous failure occurred because I didn''t save the new portfolio. But when I un-comment portfolio_test.rb:8, I get a new error: NoMethodError: You have a nil object when you didn''t expect it! The error occurred while evaluating nil.upcase! [snip] app/models/portfolio.rb:9 Thanks in advance for any light you can shine on my errors, Richard --~--~---------~--~----~------------~-------~--~----~ 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
2008-Jul-09 15:44 UTC
Re: Simple app works fine but unit tests don''t work as intended
On 9 Jul 2008, at 16:30, RichardOnRails wrote:> > I have a simple rails 2.0.2 app with one model, Portfolio, and > generated CRUD. I''ve added validation to the model and tested it > thoroughly by hand -- it works fine. > > I tried to create a unit test to ensure no inadvertent future changes > break my validations.Because you shouldn''t use an attr_accessor with the same name as a database attributes. Those attributes are not instance variables. Calling attr_accessor replaces (this is a slight simplification, but effectively this is what happens) the .symbol method that rails created to read that attribute with one that reads from an instance variable. Nothing however populates that instance variable. Fred> > > The relevant code is at http://www.pastie.org/230714. > > Question 1: > ruby test/unit/portfolio_test.rb fails with (essentially) > portfolio_test_rb:13 is not true. > > Why did this fail > > Question 2: > I thought perhaps the previous failure occurred because I didn''t save > the new portfolio. But when I un-comment portfolio_test.rb:8, I get > a new error: > NoMethodError: You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.upcase! > [snip] app/models/portfolio.rb:9 > > Thanks in advance for any light you can shine on my errors, > Richard > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
RichardOnRails
2008-Jul-09 15:53 UTC
Re: Simple app works fine but unit tests don''t work as intended
Hi Fred, Wow! Your reply was substantive, comprehensive and fast! Many thanks, Richard On Jul 9, 11:44 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 Jul 2008, at 16:30, RichardOnRails wrote: > > > > > I have a simple rails 2.0.2 app with one model, Portfolio, and > > generated CRUD. I''ve added validation to the model and tested it > > thoroughly by hand -- it works fine. > > > I tried to create a unit test to ensure no inadvertent future changes > > break my validations. > > Because you shouldn''t use an attr_accessor with the same name as a > database attributes. Those attributes are not instance variables. > Calling attr_accessor replaces (this is a slight simplification, but > effectively this is what happens) the .symbol method that rails > created to read that attribute with one that reads from an instance > variable. Nothing however populates that instance variable. > > Fred > > > > > The relevant code is athttp://www.pastie.org/230714. > > > Question 1: > > ruby test/unit/portfolio_test.rb fails with (essentially) > > portfolio_test_rb:13 is not true. > > > Why did this fail > > > Question 2: > > I thought perhaps the previous failure occurred because I didn''t save > > the new portfolio. But when I un-comment portfolio_test.rb:8, I get > > a new error: > > NoMethodError: You have a nil object when you didn''t expect it! > > The error occurred while evaluating nil.upcase! > > [snip] app/models/portfolio.rb:9 > > > Thanks in advance for any light you can shine on my errors, > > Richard--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
RichardOnRails
2008-Jul-09 15:54 UTC
Re: Simple app works fine but unit tests don''t work as intended
Hi Fred, Wow! Your reply was substantive, comprehensive and fast! Many thanks, Richard On Jul 9, 11:44 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 Jul 2008, at 16:30, RichardOnRails wrote: > > > > > I have a simple rails 2.0.2 app with one model, Portfolio, and > > generated CRUD. I''ve added validation to the model and tested it > > thoroughly by hand -- it works fine. > > > I tried to create a unit test to ensure no inadvertent future changes > > break my validations. > > Because you shouldn''t use an attr_accessor with the same name as a > database attributes. Those attributes are not instance variables. > Calling attr_accessor replaces (this is a slight simplification, but > effectively this is what happens) the .symbol method that rails > created to read that attribute with one that reads from an instance > variable. Nothing however populates that instance variable. > > Fred > > > > > The relevant code is athttp://www.pastie.org/230714. > > > Question 1: > > ruby test/unit/portfolio_test.rb fails with (essentially) > > portfolio_test_rb:13 is not true. > > > Why did this fail > > > Question 2: > > I thought perhaps the previous failure occurred because I didn''t save > > the new portfolio. But when I un-comment portfolio_test.rb:8, I get > > a new error: > > NoMethodError: You have a nil object when you didn''t expect it! > > The error occurred while evaluating nil.upcase! > > [snip] app/models/portfolio.rb:9 > > > Thanks in advance for any light you can shine on my errors, > > Richard--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---