Functional test question

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

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

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

" assert_redirected_to task_path(assigns(:task)) "

when i put this…i get undefined task_path…where do i define that??

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

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
[email protected]