# How to test login action in a controller?

**URL:** <https://www.ruby-forum.com/t/how-to-test-login-action-in-a-controller/189762>\
**Category:** Rails\
**Created:** [June 7, 2010, 10:09am UTC](https://www.ruby-forum.com/t/how-to-test-login-action-in-a-controller/189762 "2010-06-07T10:09:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSSSSS\_S\_SSSSS\_SSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@SSSSSSS\_S\_SSSSS\_SSS](https://www.ruby-forum.com/u/SSSSSSS_S_SSSSS_SSS)\
**Post date:** [June 7, 2010, 10:09am UTC](https://www.ruby-forum.com/t/how-to-test-login-action-in-a-controller/189762/1 "2010-06-07T10:09:20Z")

</div>

_My login page has a login form with three fields:login,password,and  
image  
verification code,now how to test it?_

require File.dirname( **FILE** ) + “/…/spec\_helper”

# 

[http://www.kirkbushell.com/articles/testing-using-rspec-part-2-the-controller](http://www.kirkbushell.com/articles/testing-using-rspec-part-2-the-controller)

# [http://blog.enjoyrails.com/2008/02/18/rspec-basic/](http://blog.enjoyrails.com/2008/02/18/rspec-basic/)

# 

[http://rails.anyware-technologies.com.br/2009/04/21/tdd-on-rails-4-rspec-authlogic-factory\_girl-and-resource\_controller/](http://rails.anyware-technologies.com.br/2009/04/21/tdd-on-rails-4-rspec-authlogic-factory_girl-and-resource_controller/)

# [http://matthewkwilliams.com/index.php/tag/rspec/](http://matthewkwilliams.com/index.php/tag/rspec/)

describe AdminController do  
integrate\_views

before(:each) do  
@params = {:login =\> ‘username’, :pass =\> ‘password’, :code\_image =\>  
“1234”}  
end

it “should redirect after input login and pass” do  
session.should\_receive(:[]).with(:code).and\_return(“1234”) # this  
does’nt work!!!  
#session.stub!(:[]).with(:code).and\_return “1234”  
Admin.should\_receive(:authenticate).with(params[:login],  
params[:pass]).and\_return(true)  
post ‘/’, @params  
response.should be\_redirect  
end

end

é”™è¯¯æç¤ºæ˜¯ï¼š  
Spec::Mocks::MockExpectationError in ‘AdminController should redirect  
after  
input login and pass’  
#ActionController::TestSession:0x9e77198 received :[] with unexpected  
arguments  
expected: (:code)  
got: (“flash”)  
/home/qichunren/gems/actionpack-2.1.0/lib/action\_controller/flash.rb:157:in  
`flash_without_components' /home/qichunren/gems/actionpack-2.1.0/lib/action_controller/components.rb:105:in `flash’  
/home/qichunren/gems/actionpack-2.1.0/lib/action\_controller/flash.rb:167:in  
`assign_shortcuts' /home/qichunren/gems/actionpack-2.1.0/lib/action_controller/base.rb:523:in `process\_without\_filters’  
/home/qichunren/gems/actionpack-2.1.0/lib/action\_controller/filters.rb:569:in  
`process_without_session_management_support' /home/qichunren/gems/actionpack-2.1.0/lib/action_controller/session_management.rb:130:in `process\_without\_test’  
/home/qichunren/gems/actionpack-2.1.0/lib/action\_controller/test\_process.rb:16:in  
`process' /home/qichunren/gems/actionpack-2.1.0/lib/action_controller/test_process.rb:394:in `process’  
/home/qichunren/gems/actionpack-2.1.0/lib/action\_controller/test\_process.rb:365:in  
`post’  
./spec/controllers/admin\_controller\_test.rb:31:

Finished in 0.023053 seconds
