# Failing functional tests when controllers work

**URL:** <https://www.ruby-forum.com/t/failing-functional-tests-when-controllers-work/129940>\
**Category:** Rails\
**Created:** [February 22, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/failing-functional-tests-when-controllers-work/129940 "2008-02-22T22:15:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjmorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@pjmorse](https://www.ruby-forum.com/u/pjmorse)\
**Post date:** [February 22, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/failing-functional-tests-when-controllers-work/129940/1 "2008-02-22T22:15:44Z")

</div>

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 =\>  
‘removed\_email\_address@domain.invalid’}, {: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

---

<div class="post-metadata">

**Author:** ![pjmorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@pjmorse](https://www.ruby-forum.com/u/pjmorse)\
**Post date:** [February 22, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/failing-functional-tests-when-controllers-work/129940/2 "2008-02-22T22:38:47Z")

</div>

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 =\>  
> ‘removed\_email\_address@domain.invalid’}, {: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

---

<div class="post-metadata">

**Author:** ![pjmorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@pjmorse](https://www.ruby-forum.com/u/pjmorse)\
**Post date:** [February 22, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/failing-functional-tests-when-controllers-work/129940/3 "2008-02-22T22:44:12Z")

</div>

pjmorse wrote:

> ```
> if @user.save
> # Tests never reach this code
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![pjmorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@pjmorse](https://www.ruby-forum.com/u/pjmorse)\
**Post date:** [February 22, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/failing-functional-tests-when-controllers-work/129940/4 "2008-02-22T22:47:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pjmorse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@pjmorse](https://www.ruby-forum.com/u/pjmorse)\
**Post date:** [February 22, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/failing-functional-tests-when-controllers-work/129940/5 "2008-02-22T22:54:13Z")

</div>

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 =\>  
‘removed\_email\_address@domain.invalid’}}, {: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!
