bra*_*boy 2 authentication testing login ruby-on-rails
我是在Rails环境中编写测试的新手.我正在尝试为已编写的代码编写测试.我正在尝试编写一个简单的登录测试,如下所示.
test "login with invalid credentials" do
post :login, :coach => {:user_name => 'foo', :password =>'bar'}
assert_equals flash[:error] , "Authentication failed"
end
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此测试时,我应该收到Authentication failed消息.但我得到了一个错误.
1) Error:
test_login_with_invalid_credentials(CPControllerTest):
RS::LDAPAuthentication::ConnectionError: Net::LDAP::LdapError: Server 127.0.0.1 refused connection on port 1389.
Run Code Online (Sandbox Code Playgroud)
这是Application Controller中的登录方法
def login
self.page_title = 'Sign in'
if logged_in? # already signed in
session[:return_to] = nil
redirect_to_appropriate_url
elsif params[:coach]
user_name, password = params[:coach][:user_name], params[:coach][:password]
if self.current_user = User.authenticate(user_name, password) #PLACE WHERE EXCEPTION IS RAISED
self.notice = "Signed in successfully"
redirect_to_appropriate_url
else
flash[:error] = "Authentication failed"
end
end
render :template => '/coach_portal/login' unless performed?
end
Run Code Online (Sandbox Code Playgroud)
原因很简单,登录方法是尝试与外部LDAP服务器通信,而我的测试无法连接到它.在这种情况下我该怎么办 我应该更改我的代码还是应该模拟LDAP连接.即使我模拟了一个,我该怎么做?
指针/帮助/链接非常感谢!!
为了隔离应用程序的行为,我建议删除User#authenticate方法.这样,您只测试LDAP成功,失败甚至引发异常时应用程序的行为方式.
test "login with invalid credentials" do
User.stubs(:authenticate).returns false
post :login, :coach => {:user_name => 'foo', :password =>'bar'}
assert_equals flash[:error] , "Authentication failed"
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3753 次 |
| 最近记录: |