Failing functional tests when controllers work

pjmorse
Fri, 22 Feb 2008 21:15:11 -0000

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 =>
''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,
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