# Replicating Form Behaviour in Functional Test

**URL:** <https://www.ruby-forum.com/t/replicating-form-behaviour-in-functional-test/154951>\
**Category:** Rails\
**Created:** [December 5, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/replicating-form-behaviour-in-functional-test/154951 "2008-12-05T15:03:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 5, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/replicating-form-behaviour-in-functional-test/154951/1 "2008-12-05T15:03:22Z")

</div>

Hi,

I’m really struggling getting a controller test to pass while the form  
that uses the method operates correctly.

I would really welcome a second pair of eyes to help me find out what  
I’m missing.

The test

def test\_set\_password  
@request.session[:user] = users(:admin)

```
put :set_password, :id => users(:one).id, :user => {:password =>

```

“newpass”, :password\_confirmation =\> “newpass”}

```
assert User.authenticate(users(:one).username, "newpass")
assert_redirected_to user_path(assigns(:user))

```

end

The controller method

# POST

def set\_password  
@user = User.find(params[:id])

```
respond_to do |format|
  if @user.update_attributes(params[:user])
    flash[:notice] = 'Password updated.'
    format.html { redirect_to(@user) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit_password" }
    format.xml { render :xml => @user.errors, :status

```

=\> :unprocessable\_entity }  
end  
end  
end

The view: edit\_password.html.erb

\<%= error\_messages\_for :user %\>

### \<%= @user.username%\>

\<% form\_for @user, :url =\> {:id =\> @user.id, :action =\>  
“set\_password”} do |f| %\>

\<%= f.label :password %\>  
 \<%= f.password\_field :password %\>

\<%= f.label :password\_confirmation, "Confirm Password" %\>  
 \<%= f.password\_field :password\_confirmation %\>

\<%= submit\_tag "Change" %\>

\<% end %\>

\<%= link\_to ‘Back’, @user %\>

The view works but the test fails.

Thanks in advance,

Adam

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 5, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/replicating-form-behaviour-in-functional-test/154951/2 "2008-12-05T16:28:17Z")

</div>

On 5 Dec 2008, at 14:02, Adam wrote:

> The view works but the test fails.

You need to work out why. stick a breakpoint in the action (or in  
whatever before filters may be relevant) and see what happens

Fred

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 6, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/replicating-form-behaviour-in-functional-test/154951/3 "2008-12-06T14:00:52Z")

</div>

OK, thanks Fred. I guess it’s nothing obvious which is sort of  
reassuring.

Adam

On Dec 5, 3:27 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 6, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/replicating-form-behaviour-in-functional-test/154951/4 "2008-12-06T14:09:39Z")

</div>

On Dec 6, 12:59 pm, Adam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OK, thanks Fred. I guess it’s nothing obvious which is sort of  
> reassuring.

Well there is one thing that I refrained from commenting upon because  
I’ve no idea how you do your authentication, but in my apps

@request.session[:user] = users(:admin)

would be

@request.session[:user] = users(:admin).id

But that should be quite obvious if you look at test.log (it would say  
that processing was halted because the authorization filter redirected  
or rendered)

Fred

---

<div class="post-metadata">

**Author:** ![adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@adam](https://www.ruby-forum.com/u/adam)\
**Post date:** [December 6, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/replicating-form-behaviour-in-functional-test/154951/5 "2008-12-06T14:34:52Z")

</div>

OK, thanks for that advice.

It’s not the problem the test is railing on the first assertion. It’s  
the update of the user record that’s failing for some reason.

Adam

On Dec 6, 1:08 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
