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
>
> 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
>
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
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/632e78b8-b6eb-4124-a2bb-6050dfb66118%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.