Hey i am new to testing on rails and i have the following test written
describe ‘GET search’ do
it ‘finds a named account directly’ do
get :search, name: ‘test’
expect(response).to redirect_to(account_path(‘test’))
end
I have been getting the following error
AccountsController GET search finds a named account directly
Failure/Error: expect(response).to
redirect_to(account_path(‘test’))
Expected response to be a <:redirect>, but was <200>
# ./spec/controllers/account_controller_spec.rb:21:in `block (3
levels) in <top (required)>’
I however get a response status of 200. I am not sure what i am doing
wrong cause all my links however work correctly. Is there a way to debug
this or find where the actual response is headed to ?
kaushik p. wrote in post #1100577:
Hey i am new to testing on rails and i have the following test written
describe ‘GET search’ do
it ‘finds a named account directly’ do
get :search, name: ‘test’
expect(response).to redirect_to(account_path(‘test’))
end
I have been getting the following error
AccountsController GET search finds a named account directly
Failure/Error: expect(response).to
redirect_to(account_path(‘test’))
Expected response to be a <:redirect>, but was <200>
# ./spec/controllers/account_controller_spec.rb:21:in `block (3
levels) in <top (required)>’
I however get a response status of 200. I am not sure what i am doing
wrong cause all my links however work correctly. Is there a way to debug
this or find where the actual response is headed to ?
Your spec clearly indicates that you expect the “search” action to
respond with a redirect (status 301 or 302), but the response is
actually 200 - SUCCESS.
I don’t see your controller action code posted here, but I can guess
from the spec failure that it is not performing a redirect.
It’s pretty easy to confirm whether a redirect is occurring with curl…
Example:
$ curl -v http://example.com
- About to connect() to example.com port 80 (#0)
- Trying 192.0.43.10… connected
- Connected to example.com (192.0.43.10) port 80 (#0)
GET / HTTP/1.1
User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4
OpenSSL/0.9.8r zlib/1.2.5
Host: example.com
Accept: /
- HTTP 1.0, assume close after body
< HTTP/1.0 302 Found
< Location: IANA-managed Reserved Domains
< Server: BigIP
- HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
<
- Connection #0 to host example.com left intact
- Closing connection #0
Hey Thank you very much for your reply.
I tried doing a curl as u said and i do get a 302 response
GET /accounts/search?name= HTTP/1.1
User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4
OpenSSL/0.9.8r zlib/1.2.5
Host: localhost:3000
Accept: /
< HTTP/1.1 302 Found
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: -1
< Location: http://localhost:3000/login?referer=/accounts/search?name=
< Content-Type: text/html; charset=utf-8
< X-Ua-Compatible: IE=Edge
< X-Request-Id: 53965c374ddaeab58ccb3bc1579058e6
< X-Runtime: 0.005424
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-02-06)
< Date: Thu, 07 Mar 2013 19:20:10 GMT
< Content-Length: 131
< Connection: Keep-Alive
Here is my controller code for search
coding: UTF-8
class AccountsController < ApplicationController
def search
@accounts = []
client = AccountsClient.new
if params[:name]
response = client.query_account(CGI.escape(params[:name]))
if response.is_a?(Net::HTTPSuccess)
account = JSON.parse(response.body)
unless account.empty?
session[:account] = account
redirect_to account_path(params[:name])
end
end
limit=100
limit = params[:limit].to_i if
params[:limit].to_i.between?(1,100)
offset = ''
offset_param = params[:offset]
if is_integer?(offset_param)
offset = "&offset=#{params[:offset]}" if
offset_param.to_i.between?(0, 2**16)
end
query =
“?name=#{CGI.escape(params[:name])}&limit=#{limit}#{offset}”
response = client.query_account(query)
end
end
end
Still can’t figure out why it isn’t redirecting to the
account_path(params[:name])