# Response\_assertions.rb: TypeError: can't convert Hash into String

**URL:** <https://www.ruby-forum.com/t/response-assertions-rb-typeerror-cant-convert-hash-into-string/134032>\
**Category:** Rails\
**Created:** [April 7, 2008, 3:28am UTC](https://www.ruby-forum.com/t/response-assertions-rb-typeerror-cant-convert-hash-into-string/134032 "2008-04-07T03:28:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![foudfou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@foudfou](https://www.ruby-forum.com/u/foudfou)\
**Post date:** [April 7, 2008, 3:28am UTC](https://www.ruby-forum.com/t/response-assertions-rb-typeerror-cant-convert-hash-into-string/134032/1 "2008-04-07T03:28:38Z")

</div>

Hello,

I’m getting an error while testing my AWDWR2 application :

1. Error:  
test\_login(LoginControllerTest):  
TypeError: can’t convert Hash into String  
/home/foudil/src/ror/depot/vendor/rails/actionpack/lib/  
action\_controller/assertions/response\_assertions.rb:115:in `=~' /home/foudil/src/ror/depot/vendor/rails/actionpack/lib/ action_controller/assertions/response_assertions.rb:115:in`assert\_redirected\_to’  
/home/foudil/src/ror/depot/vendor/rails/actionpack/lib/  
action\_controller/assertions/response\_assertions.rb:114:in `collect' /home/foudil/src/ror/depot/vendor/rails/actionpack/lib/ action_controller/assertions/response_assertions.rb:114:in`assert\_redirected\_to’  
/home/foudil/src/ror/depot/vendor/rails/actionpack/lib/  
action\_controller/assertions.rb:54:in `clean_backtrace' /home/foudil/src/ror/depot/vendor/rails/actionpack/lib/ action_controller/assertions/response_assertions.rb:54:in`assert\_redirected\_to’  
./test/functional/login\_controller\_test.rb:25:in `test_login' /home/foudil/src/ror/depot/vendor/rails/activerecord/lib/../../ activesupport/lib/active_support/testing/default.rb:7:in`run’

in test/functional/login\_controller\_test.rb:

#…  
def test\_login  
dave = users(:dave)  
post :login, :name =\> dave.name, :password =\> ‘secret’  
assert\_redirected\_to :action =\> “index”  
assert\_equal dave.id, session[:user\_id]  
end  
#…

in response\_assertions.rb:

```
  #...

```

112 rescue ActionController::RoutingError # routing failed  
us, so match the strings only.  
113 msg = build\_message(message, "expected a redirect to \<?

> , found one to \<?\>", options, @response.redirect\_url)  
> 114 url\_regexp = %r{^(\w+://._?(/|$|?))(._)$}  
> 115 eurl, epath, url, path = [options,  
> @response.redirect\_url].collect do |url|  
> 116 u, p = (url\_regexp =~ url) ? [$1, $3] : [nil, url]  
> 117 [u, (p.first == ‘/’) ? p : ‘/’ + p]  
> 118 end.flatten  
> #…

The problem seems to be that the “option” variable is a hash, in my  
case :  
{:action =\> :login}  
and thus, it can not be matched against a regex

Is this a bug, or am I misusing something ?

Ruby version 1.8.6  
Rails version 2.0.2

---

<div class="post-metadata">

**Author:** ![foudfou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@foudfou](https://www.ruby-forum.com/u/foudfou)\
**Post date:** [April 8, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/response-assertions-rb-typeerror-cant-convert-hash-into-string/134032/2 "2008-04-08T18:33:51Z")

</div>

Ok I think I found it : the problem comes from the “post” instruction,  
not from “assert\_redirect\_to” !

I should have written :

```
post :login, {}, { :name => dave.name, :password => 'secret' }
```

---

<div class="post-metadata">

**Author:** ![foudfou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@foudfou](https://www.ruby-forum.com/u/foudfou)\
**Post date:** [April 9, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/response-assertions-rb-typeerror-cant-convert-hash-into-string/134032/3 "2008-04-09T17:21:27Z")

</div>

No. This was a wrong solution.

because the parameters are passed as session information (non-sense).

The original problem comes from ssl\_requirement used in the login  
controller, which redirects to the same URI, but with ssl.  
Thus making the assert\_redirected\_to break down.

Which leads me to another question : how to test redirection to  
https://…  
in the functional tests ?  
🙂

Further information underneath :

#…  
class LoginController \< ApplicationController

layout “products”  
before\_filter :authorize, :except =\> :login  
ssl\_required :login, :add\_user  
#…  
def login  
session[:user\_id] = nil  
if request.post?  
user = User.authenticate(params[:name], params[:password])  
if user  
session[:user\_id] = user.id  
uri = session[:original\_uri]  
session[:original\_uri] = nil  
redirect\_to(uri || { :action =\> “index” })  
else  
flash[:notice] = “Nom de l’utilisateur ou mot de passe  
incorrect”  
end  
end  
end  
#…

@request.headers:

REQUEST\_URI: /login/login?name=dave&password=secret  
SERVER\_PORT: 80  
REQUEST\_METHOD: POST

@request.response:

Status: 302 Found  
X-Runtime: 0.00129  
Location: [https://test.host/login/login?name=dave&password=secret](https://test.host/login/login?name=dave&password=secret)  
type: text/html; charset=utf-8  
cookie:  
Cache-Control: no-cache  
Content-Length: 121

---

<div class="post-metadata">

**Author:** ![foudfou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@foudfou](https://www.ruby-forum.com/u/foudfou)\
**Post date:** [April 9, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/response-assertions-rb-typeerror-cant-convert-hash-into-string/134032/4 "2008-04-09T17:28:29Z")

</div>

On 9 Apr 2008, at 16:20, foudfou wrote:

> No. This was a wrong solution.
> 
> because the parameters are passed as session information (non-sense).
> 
> The original problem comes from ssl\_requirement used in the login  
> controller, which redirects to the same URI, but with ssl.  
> Thus making the assert\_redirected\_to break down.

This seems vaguely familiar. IIRC, if the parameter passed to  
redirect\_to is a string, then the parameter passed to  
assert\_redirected\_to also needs to be a string: rails knows how to  
compare strings, it knows how to compare hashes, but it won’t compare  
a string to a hash for you.

Fred
