Hello Rubyists, I''m a bit stumped on this one. I have a signup controller that lets people add their name, email address and a passcode to a mailing list. When I go through the UI, everything works as I would expect - a record is added to the appropriate table in the database, and the flash sends the greeting I want to the next page. But when I run my functional tests, they blow up! Not sure what to do at this point, so hopefully a kind soul on here can point me in the right direction. Okay, the code follows below. There''s no code in the model other than the usual "validates'' methods, so I didn''t include it. Error message: 1) Failure: test_sign_up_adds_new_recipient(SignupControllerTest) [/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:26:in `assert_response'' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:18:in `assert_response'' /home/kodama/blog/test/functional/signup_controller_test.rb:20:in `test_sign_up_adds_new_recipient'']: Expected response to be a <:redirect>, but was <200> My test method: (all the asserts fail) def test_sign_up_adds_new_recipient num_recipients = Recipient.count post :signup, :signup => {:name => "test", :email_address => "test-J0of1frlU80@public.gmane.org",:passcode => "test"} assert_response :redirect assert_equal "Thank you for signing up!", flash[:notice] assert_equal num_recipients + 1, Recipient.count end My controller code: (standard controller, nothing special other than this method) #Add a new recipient through the sign-up sheet def signup @recipient = Recipient.new(params[:signup]) if @recipient.save flash[:notice] = "Thank you for signing up!" redirect_to :controller => ''read'', :action => ''list'' else render :action => ''new'' end end My view code: (signup/new.rhtml) <% form_tag :action => ''signup'' do %> <%= render :partial => ''signup'' %> <label><%= submit_tag "Sign me up!" %></label> <% end %> The partial: Thanks, kodama -- 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 -~----------~----~----~----~------~----~------~--~---
There''s nothing I can see from the code from your example that is wrong. Obviously, the object is not saving so I suspect that the parameters you are passing are incorrect and failing validation. Use an assert_valid to find out where the validation errors are. http://api.rubyonrails.org/classes/ActionController/Assertions/ModelAssertions.html e.g. assert_valid(assigns(:recipient)) On Nov 17, 4:54 pm, Old Echo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello Rubyists, > I''m a bit stumped on this one. I have a signup controller that lets > people add their name, email address and a passcode to a mailing list. > When I go through the UI, everything works as I would expect - a record > is added to the appropriate table in the database, and the flash sends > the greeting I want to the next page. But when I run my functional > tests, they blow up! Not sure what to do at this point, so hopefully a > kind soul on here can point me in the right direction. > > Okay, the code follows below. There''s no code in the model other than > the usual "validates'' methods, so I didn''t include it. > > Error message: > > 1) Failure: > test_sign_up_adds_new_recipient(SignupControllerTest) > [/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:26:in > `assert_response'' > /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:18:in > `assert_response'' > /home/kodama/blog/test/functional/signup_controller_test.rb:20:in > `test_sign_up_adds_new_recipient'']: > Expected response to be a <:redirect>, but was <200> > > My test method: (all the asserts fail) > > def test_sign_up_adds_new_recipient > num_recipients = Recipient.count > post :signup, :signup => {:name => "test", :email_address => > "t...-J0of1frlU80@public.gmane.org",:passcode => "test"} > assert_response :redirect > assert_equal "Thank you for signing up!", flash[:notice] > assert_equal num_recipients + 1, Recipient.count > end > > My controller code: (standard controller, nothing special other than > this method) > > #Add a new recipient through the sign-up sheet > def signup > @recipient = Recipient.new(params[:signup]) > if @recipient.save > flash[:notice] = "Thank you for signing up!" > redirect_to :controller => ''read'', :action => ''list'' > else > render :action => ''new'' > end > end > > My view code: (signup/new.rhtml) > > <% form_tag :action => ''signup'' do %> > <%= render :partial => ''signup'' %> > <label><%= submit_tag "Sign me up!" %></label> > <% end %> > > The partial: > > Thanks, > kodama > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
Nicholas Henry wrote:> There''s nothing I can see from the code from your example that is > wrong. Obviously, the object is not saving so I suspect that the > parameters you are passing are incorrect and failing validation. Use > an assert_valid to find out where the validation errors are. > > http://api.rubyonrails.org/classes/ActionController/Assertions/ModelAssertions.html > > e.g. assert_valid(assigns(:recipient))Thank you so much! The issue was that I was violating uniqueness constraints...I think I left stale data in there, or I have to go double-check my fixtures. In either case, this fixed the problem: #Add Time.now to name to guarantee uniqueness post :signup, :signup => {:name => "test#{Time.now}", :email_address => "test-J0of1frlU80@public.gmane.org",:passcode => "test"} Thanks again! :) -- 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 -~----------~----~----~----~------~----~------~--~---