On Thu, Jan 1, 2009 at 5:25 PM, Vahagn Hayrapetyan <
[email protected]> wrote:
>
> 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
>
> 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.[]">
>
>
It's funny you should ask this, as I spent this morning trying to figure
this out myself for my project. Your post in #test_forgot_password needs to
look something like:
post 'account/forgot', :user => {:email => '[email protected]'}
I figured this out by the judicious use of print statements in my controller
printing out the values of options, options[:user], and
options[:user][:email].
Hope this helps.
--wpd
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---