Help! I have a new/create process that works fine in the actual app. But when I test I always get a failure in the test_create section. Here''s some controller code: def new end def create begin @borrower = Borrower.new(params[:borrower]) if @borrower.save flash[:notice] = ''Borrower '' + @borrower.FirstName + '' '' + @borrower.LastName + '' was added.'' redirect_to :action => ''list'' else flash[:notice] = ''Borrower could not be added. Must have a Last Name and a unique SSN.'' render :action => ''new'' end rescue flash[:notice] = ''Borrower could not be added.'' render :action => ''new'' end end Setting breakpoints at different places in the above code indicates that my test is going through the rescue section. Here''s the test code: def test_new get :new assert_response :success assert_template ''new'' end def test_create num_borrowers = Borrower.count post :create, :borrower => {:SSN => ''000'', :LastName => ''Anyone''} assert_response :redirect # gets 200 (OK) instead of 302 (Found) assert_redirected_to :action => ''list'' assert_equal ''Borrower Anyone was added.'', flash[:notice] assert_equal num_borrowers + 1, Borrower.count end What am I leaving out of my test to keep me from :success? Or should it be rearranged somehow? As I said, the app itself adds a borrower correctly. TIA, Shauna -- 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 -~----------~----~----~----~------~----~------~--~---
Get freelance or telecom-job here!! http://freelance.telecom.googlepages.com On Oct 21, 10:43 am, Shauna <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Help! > > I have a new/create process that works fine in the actual app. But when > I test I always get a failure in the test_create section. Here''s some > controller code: > > def new > end > > def create > begin > @borrower = Borrower.new(params[:borrower]) > if @borrower.save > flash[:notice] = ''Borrower '' + @borrower.FirstName + '' '' + > @borrower.LastName + '' was added.'' > redirect_to :action => ''list'' > else > flash[:notice] = ''Borrower could not be added. Must have a Last > Name and a unique SSN.'' > render :action => ''new'' > end > rescue > flash[:notice] = ''Borrower could not be added.'' > render :action => ''new'' > end > end > > Setting breakpoints at different places in the above code indicates that > my test is going through the rescue section. > > Here''s the test code: > def test_new > get :new > assert_response :success > assert_template ''new'' > end > def test_create > num_borrowers = Borrower.count > post :create, :borrower => {:SSN => ''000'', :LastName => ''Anyone''} > assert_response :redirect # gets 200 (OK) instead of 302 > (Found) > assert_redirected_to :action => ''list'' > assert_equal ''Borrower Anyone was added.'', flash[:notice] > assert_equal num_borrowers + 1, Borrower.count > end > > What am I leaving out of my test to keep me from :success? Or should it > be rearranged somehow? As I said, the app itself adds a borrower > correctly. > > TIA, > Shauna > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Hi Shauna, I would recommend doing the following for your test_create: def test_create # obtain the number of borrowers. num_borrowers = Borrower.count # post the data to the server post :create, :borrower => {:SSN => ''000'', :LastName => ''Anyone''} # was the response successful. assert_response :success # did we get redirected to the correct page. assert_redirected_to :action => ''list'' # did the appropriate message get displayed on success. assert_equal ''Borrower Anyone was added.'', flash[:notice] # did the number of borrowers get incremented. assert_equal num_borrowers + 1, Borrower.count end I''m new to RoR and I have also completed the ''Ruby on Rails'' course recently in Dallas. Good luck, -Conrad On 10/20/06, IT.Telecom <freelance.telecom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Get freelance or telecom-job here!! > http://freelance.telecom.googlepages.com > > On Oct 21, 10:43 am, Shauna <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > Help! > > > > I have a new/create process that works fine in the actual app. But when > > I test I always get a failure in the test_create section. Here''s some > > controller code: > > > > def new > > end > > > > def create > > begin > > @borrower = Borrower.new(params[:borrower]) > > if @borrower.save > > flash[:notice] = ''Borrower '' + @borrower.FirstName + '' '' + > > @borrower.LastName + '' was added.'' > > redirect_to :action => ''list'' > > else > > flash[:notice] = ''Borrower could not be added. Must have a Last > > Name and a unique SSN.'' > > render :action => ''new'' > > end > > rescue > > flash[:notice] = ''Borrower could not be added.'' > > render :action => ''new'' > > end > > end > > > > Setting breakpoints at different places in the above code indicates that > > my test is going through the rescue section. > > > > Here''s the test code: > > def test_new > > get :new > > assert_response :success > > assert_template ''new'' > > end > > def test_create > > num_borrowers = Borrower.count > > post :create, :borrower => {:SSN => ''000'', :LastName => ''Anyone''} > > assert_response :redirect # gets 200 (OK) instead of 302 > > (Found) > > assert_redirected_to :action => ''list'' > > assert_equal ''Borrower Anyone was added.'', flash[:notice] > > assert_equal num_borrowers + 1, Borrower.count > > end > > > > What am I leaving out of my test to keep me from :success? Or should it > > be rearranged somehow? As I said, the app itself adds a borrower > > correctly. > > > > TIA, > > Shauna > > > > -- > > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Thanks for responding, Conrad. I substituted the line assert_response :success as you suggested, and now the Failure message reads Expected response to be a <:redirect>, but was <200> In other words, the :success test passes but Rails still chokes on the following line. Does anyone know why the test goes through the rescue section while my app itself does not? I''ve been tearing my hair out for days over this one! Thanks, Shauna -- 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 -~----------~----~----~----~------~----~------~--~---
Hi...I''m pretty new to rails as well, but maybe I can help. The response code will be either 200 (success) or 302 (redirect). Not both. So, you''re orignal test code looks good to me. Unfortunatley, I don''t know what the problem is though. Or maybe try adding some code to see if the Borrower was created properly, before saving, just to doublecheck that the parameters were passed in properly. @borrower = Borrower.new(params[:borrower]) puts "Borrower SSN: #{@borrower.LastName}" # doublecheck the new borrowers LastName if @borrower.save Is it possible that "anyone" is already in the db? Good Luck! On Oct 23, 12:09 am, Shauna <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks for responding, Conrad. I substituted the line > assert_response :success > as you suggested, and now the Failure message reads > Expected response to be a <:redirect>, but was <200> > > In other words, the :success test passes but Rails still chokes on the > following line. > > Does anyone know why the test goes through the rescue section while my > app itself does not? > > I''ve been tearing my hair out for days over this one! > > Thanks, > Shauna > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Hi Chris, Thanks for the suggestion. Yes, the @borrower.LastName comes out correctly in both the test and the real app. So I''m back to -- WHY does the test go through the rescue section while the app itself executes correctly? Can I get the attention of anyone who knows what''s going on here? I''ve used Google Code Search to see if anyone else does the test_new ... test_create sequence this way. The only one I found was iteration15 of pomodo, which like the Agile examples, has no fixture data to actually do a test with. Has anyone actually tested this part of the Agile code in their app??? I should clarify that in the code above, test_new also fails (in test only, app is fine), but if I add the (duplicate) line @borrower = Borrower.new(params[:borrower]) to test_new, the test doesn''t fail and the app still works fine. I''m sure I''m missing something here, and I know I sure do miss my traditional debugging tools! Shauna -- 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 -~----------~----~----~----~------~----~------~--~---
Sorry, I meant to say, if I add the (duplicate) line @borrower = Borrower.new(params[:borrower]) to def new, the test doesn''t fail and the app still works fine. Shauna -- 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 -~----------~----~----~----~------~----~------~--~---