Method works but tests fail?

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:inassert_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 =>
[email protected]”,: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’ %>
<%= submit_tag “Sign me up!” %>
<% end %>

The partial:

Thanks,
kodama

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))

Nicholas H. 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 =>
[email protected]”,:passcode => “test”}

Thanks again! :slight_smile: