Re: Failing functional tests when controllers work

Ilan Berci
Fri, 22 Feb 2008 21:38:47 -0000

pjmorse wrote:

>
>   def create # Creates a user with admin role
>     @user = User.new(params[:user])
>     @user.role = "Admin"
>
>     if @user.save
>                         # Tests never reach this code
>       flash[:notice] = "User successfully created"
>       redirect_to :action => ''show'', :id =>
@user.id
>     else
>       flash[:warning] = "Please try again."
>       render :action => ''new''
>     end
>   end
>
> and the test is...
>
>   def test_create # Supposed to succeed, but fails.
>     old = User.count
>     post :create, {:username => "testuser", :password =>
> ''secret'', :password_confirmation =>
''secret'', :email =>
> ''test-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org''}, {:user
=> users(:admin1)} # Need an admin user in
> the session to allow this
> # n.b. there is a validation to check for presence of username,


> Any ideas?
>
> pjm

I think your error is before the save, in the test case, your :user is
not passing in the required fields to populate a new user.  Here is what
you can do to verify this.

1) sudo gem install ruby-debug
2) insert: "debugger" a the top of your controller''s create
method
3) run: rdebug test/script/<test case name> [<test name>]

# you are now in the debugger..
> cont    # debugger starts on first line of the file, hit cont to go to your
bp
> irb     # when you hit your bp
irb> @user


hth

ilan
--
Posted via http://www.ruby-forum.com/.