# Functional test question

**URL:** <https://www.ruby-forum.com/t/functional-test-question/117222>\
**Category:** Rails\
**Created:** [October 15, 2007, 12:05am UTC](https://www.ruby-forum.com/t/functional-test-question/117222 "2007-10-15T00:05:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [October 15, 2007, 12:05am UTC](https://www.ruby-forum.com/t/functional-test-question/117222/1 "2007-10-15T00:05:30Z")

</div>

This test of my signups controller fails on the last line: it gets  
redirected to the account/login page.

```
get :signup, :id => 1 # must be logged in to view this page
assert_redirected_to :action => 'login'
login_as :quentin
post :create, :form_data => {:first_name => "joe", :last_name =>

```

“bob”, :form\_type\_id =\> 1}  
assert\_redirected\_to :controller =\> ‘signups’, :action =\> ‘select’

The create action does require login, but if I comment out the first two  
lines, then it passes, so the login seems to work. Somehow the first get  
request is changing what happens when i call login\_as and then post to  
signups/create. I assume I just don’t understand something about why  
that would change some important state. Maybe I’m only supposed to have  
one get or post per test? Can someone explain it?

I also tried this and it passes:

```
login_as :quentin
get :signup, :id => 1
post :create, :form_data => {:first_name => "joe", :last_name =>

```

“bob”, :form\_type\_id =\> 1}  
assert\_redirected\_to :controller =\> ‘signups’, :action =\> ‘select’

So the problem is due to being redirected to another controller, I  
guess.

- Elliot

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [October 15, 2007, 3:25am UTC](https://www.ruby-forum.com/t/functional-test-question/117222/2 "2007-10-15T03:25:21Z")

</div>

Functional test you will only deal with testing one action. In  
integration tests you can test across different controllers and  
action.

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [October 15, 2007, 3:50am UTC](https://www.ruby-forum.com/t/functional-test-question/117222/3 "2007-10-15T03:50:18Z")

</div>

Bala P. wrote:

> Functional test you will only deal with testing one action. In  
> integration tests you can test across different controllers and  
> action.

I thought functional tests were supposed to focus on individual  
controllers, not individual actions. This test does not touch multiple  
controllers except that some actions redirect elsewhere and you can make  
assertions about where it’s redirected. Are you saying you can’t use get  
or post twice in one functional test, or what did I do wrong? What if  
they were both for the same action?

- Elliot

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [December 4, 2009, 10:20pm UTC](https://www.ruby-forum.com/t/functional-test-question/117222/4 "2009-12-04T22:20:14Z")

</div>

" assert\_redirected\_to task\_path(assigns(:task)) "

when i put this…i get undefined task\_path…where do i define that??

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [October 27, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/functional-test-question/117222/5 "2007-10-27T14:31:29Z")

</div>

in your test\_helper.rb

def login\_as(user)  
@request.session[:user] = user ? users(user).id : nil  
end

then in your whatever\_controller\_test.rb, here I use the  
tasks\_controller ( new and create…)  
user the login\_as… and don’t forget the fixtures

fixtures :tasks, :users

def test\_should\_get\_new  
login\_as(:quentin)  
get :new  
assert\_response :success  
end

def test\_should\_create\_task  
login\_as(:quentin)  
old\_count = Task.count

```
post :create, :task => {:user_id => 1, :category => 1, :title => 

```

‘cut the flowers’,  
:description =\> “lorem ipsum…”, :should\_start\_at =\>  
5.days.from\_now.utc, :should\_end\_at =\> 15.days.from\_now.utc }  
assert\_equal old\_count+1, Task.count

```
assert_redirected_to task_path(assigns(:task))

```

end

---

<div class="post-metadata">

**Author:** ![curi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@curi](https://www.ruby-forum.com/u/curi)\
**Post date:** [December 4, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/functional-test-question/117222/6 "2009-12-04T22:29:56Z")

</div>

Elliot T. wrote:

> Bala P. wrote:
> 
> > Functional test you will only deal with testing one action. In  
> > integration tests you can test across different controllers and  
> > action.
> 
> I thought functional tests were supposed to focus on individual  
> controllers, not individual actions.

Not really. Functional tests focus on individual high-level functions.  
But don’t use them or integration tests – replace them with Cucumber  
scenarios.

Best,  
–Â  
MarnenÂ Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
