Failing functional tests when controllers work

I have three models (users, issues, retailers) and three corresponding
controllers. In functional tests for all three controllers, I have the
same problems:

  • Creating a record which should work, fails in the functional test.
  • Updating a record which should fail (invalid) succeeds (and
    therefore the test fails.)

Other tests of these actions (e.g. a valid update, an invalid
creation) work as expected. I am unable to recreate the problem in the
console with the same data, even when using the test environment.

I would suspect a bug in the controllers except that all three
functional tests fail in the same way.

Here’s a sample with users:

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 =>
[email protected]’}, {: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,

password, and email; also to check pw confirmation
assert_not_nil assigns(:user)
assert assigns(:user).is_admin?
assert_response :redirect
assert_redirected_to :action => “show”, :id => assigns(:user).id
assert_equal “User successfully created”, flash[:notice]
assert_equal old + 1, User.count
end

The test fails at the assert_response :redirect, so it’s the
@user.save which isn’t behaving as expected. The same thing happens in
two other controllers, with two other models.

Any ideas?

pjm

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 =>
[email protected]’}, {: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/ []

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

pjmorse wrote:

if @user.save
                    # Tests never reach this code

Temporarily change that to @user.save! and read the error diagnostic?

Phlip wrote:

pjmorse wrote:

if @user.save
                    # Tests never reach this code

Temporarily change that to @user.save! and read the error diagnostic?

no need to even do that… just type @user and see what the debugger
gives you… I believe that your password fields will not be set which
therefore failed the validation tests in your model

hth

ilan

Ilan, thanks for the ruby-debug tip, I’ll need that one of these days.

For this one, I followed Phlip’s suggestion and switched to save!,
which confirmed Ilan’s suggestion that the parameters were not
correct. I needed to set the params to create a user object, e.g.

post :create, {:user => {:username => "testuser", :password =>

‘secret’, :password_confirmation => ‘secret’, :email =>
[email protected]’}}, {:user => users(:admin1)}

…not sure yet how this will work with the update bug, but it should
fix the save bug across all three tests. Thanks!