follow_redirect is not working as expected in Sinatra

179 views
Skip to first unread message

Arup Rakshit

unread,
Dec 25, 2014, 3:21:06 PM12/25/14
to rs...@googlegroups.com
Hi,

Here is my route :-

post '/export' do
gist = List.new(Task.all).to_gist
redirect gist['html_url']
end

and the spec :-

describe "/export" do
let(:gist) { double('Gist') }
let(:url) { "http://example.org/gist" }

it 'exports as a gist' do
allow_any_instance_of(List).to receive(:to_gist) { gist }
allow(gist).to receive(:[]).with('html_url').and_return(url)
post "/export"
expect(last_response).to be_redirect
follow_redirect!
expect(last_request.url).to eq(url)
end
end

Now, when I am running my test :

[arup@to_do_app]$ rspec spec/todo_spec.rb:42
Run options: include {:locations=>{"./spec/todo_spec.rb"=>[42]}}
task is
F

Failures:

1) ToDo App routes /export exports as a gist
Failure/Error: follow_redirect!
NoMethodError:
undefined method `id' for nil:NilClass
# ./views/show.erb:1:in `block in singleton class'
# ./views/show.erb:131067:in `instance_eval'
# ./views/show.erb:131067:in `singleton class'
# ./views/show.erb:131065:in `__tilt_80054700'
#
/home/arup/.rvm/gems/ruby-2.1.4@2_1_4/gems/tilt-1.4.1/lib/tilt/template.rb:170:in
`call'
#
/home/arup/.rvm/gems/ruby-2.1.4@2_1_4/gems/tilt-1.4.1/lib/tilt/template.rb:170:in
`evaluate'
#
/home/arup/.rvm/gems/ruby-2.1.4@2_1_4/gems/tilt-1.4.1/lib/tilt/template.rb:103:in
`render'
#
/home/arup/.rvm/gems/ruby-2.1.4@2_1_4/gems/sinatra-1.4.5/lib/sinatra/base.rb:814:in
`render'
#
/home/arup/.rvm/gems/ruby-2.1.4@2_1_4/gems/sinatra-1.4.5/lib/sinatra/base.rb:665:in
`erb'
# ./todo.rb:41:in `block in <class:ToDoApp>'
#
/home/arup/.rvm/gems/ruby-2.1.4@2_1_4/gems/sinatra-1.4.5/lib/sinatra/base.rb:1603:in
`call'

Why it is not working? As per this Q/A -
http://stackoverflow.com/questions/7424443/how-do-i-test-a-redirect-in-sinatra-using-rspec it should work.

Any pointer please ?


--
================
Regards,
Arup Rakshit
================
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.

--Brian Kernighan

Myron Marston

unread,
Dec 26, 2014, 6:29:54 PM12/26/14
to rs...@googlegroups.com
What makes you think `follow_redirect!` isn't working?  It looks to me like it is following the redirect, making a new request, and then your view hits an exception when rendering the response for the 2nd response.  Without seeing your code, my best guess is that there's either a bug in your sinatra route or view (causing the the `id` message to be sent to a `nil` object in your view) or your sinatra is assuming some additional state that has not been setup in your test (e.g. the existence of some records in the database, or the user being logged in, etc).

Regardless, `follow_redirect!` is not part of RSpec.  It's part of rack-test, and any questions about it should be directed to the rack-test maintainers.

HTH,
Myron 

Arup Rakshit

unread,
Dec 27, 2014, 3:43:18 AM12/27/14
to rs...@googlegroups.com
On Friday, December 26, 2014 03:29:53 PM Myron Marston wrote:
> On Thursday, December 25, 2014 12:21:06 PM UTC-8, Arup Rakshit wrote:
> > Hi,
> >
> > Here is my route :-
> >
> > post '/export' do
> >
> > gist = List.new(Task.all).to_gist
> > redirect gist['html_url']
> >
> > end
> >
> > and the spec :-
> >
> > describe "/export" do
> >
> > let(:gist) { double('Gist') }
> > let(:url) { "http://example.org/gist" }
> >
> > it 'exports as a gist' do
> >
> > allow_any_instance_of(List).to receive(:to_gist) { gist }
> > allow(gist).to receive(:[]).with('html_url').and_return(url)
> > post "/export"
> > expect(last_response).to be_redirect
> > follow_redirect!
> > expect(last_request.url).to eq(url)
> >
> > end
> >
> > end
>
> What makes you think `follow_redirect!` isn't working? It looks to me like
> it is following the redirect, making a new request, and then your view hits
> an exception when rendering the response for the 2nd response. Without
> seeing your code, my best guess is that there's either a bug in your
> sinatra route or view (causing the the `id` message to be sent to a `nil`
> object in your view) or your sinatra is assuming some additional state that
> has not been setup in your test (e.g. the existence of some records in the
> database, or the user being logged in, etc).
>
> Regardless, `follow_redirect!` is not part of RSpec. It's part of
> rack-test, and any questions about it should be directed to the rack-test
> maintainers.
>
> HTH,
> Myron

Myron,

Thanks for the reply. I'll pass it to the Rack-test list. But I thought,
follow_redirect! should follow the url that gist['html_url'] is returning, but
that's not happening. Actually it works in UI. But test is failing due to
incorrect redirection.
Reply all
Reply to author
Forward
0 new messages