Hi,-

I'm wondering how to test POSTing something to an action from a form. I
am testing an action named "forgot" in my account_controller.rb. It
takes an email address as a parameter and does things based on whether a
user with that email address exists:

def forgot
    if request.post?
      user = User.find_by_email(params[:user][:email])
      if user
        user.create_reset_code
        flash[:notice] ="Reset code sent to #{user.email}"
      else
        flash[:notice] ="#{params[:user][:email]} does not exit in the
system."
      end
      redirect_to index_url
    end
  end

The HTML looks like this:

<form action="/account/forgot" method="post"><div
style="margin:0;padding:0"><input name="authenticity_token"
type="hidden" value="b17df8ef7db06d17204142dfc501ff77256caa81" /></div>
<p>Enter email address: <br /> <input id="user_email" name="user[email]"
size="30" type="text" /></p>

  <input name="commit" type="submit" value="Email new password" />
</form>

So I'm trying to test this like:

 def test_forgot_password
    get 'account/forgot'
    assert_response :success
    assert_template 'account/forgot' #all fine so far
    post 'account/forgot', '[email protected]' #not good
    assert_redirected_to index_url #fails
 end

I get:

Expected response to be a <:redirect>, but was <500>
<"You have a nil object when you didn't expect it!\nYou might have
expected an instance of ActiveRecord::Base.\nThe error occurred while
evaluating nil.[]">

I'm not sure if the request parameter is wrongly formed? The app must
redirect to the index url regardless of the validity of the email
address. So the error must lie elsewhere I guess!

Thanks and oh, a Happy New (Rails) Year :-)
-- 
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to