Anonymous controller problem #573

Closed
Exoth opened this Issue Jul 17, 2012 · 15 comments

Projects

None yet

7 participants

@Exoth
Exoth commented Jul 17, 2012

I've got such a before_filter in ApplicationController:

def foo
  redirect_to(params.except(:ref, :marker), status: 301) if params[:ref] || params[:marker]
end

And I want to test it using anonymous controller like this:

require 'spec_helper'

describe ApplicationController, type: :controller do
  controller do
    def index
      head :ok
    end
  end

  it "should redirect without ref and marker if any of them is given in request" do
    get :index, ref: 1, marker: 2, foo: 3
    response.should redirect_to('/anonymous/?foo=3')
  end
end

But this results in "was a redirect to http://test.host/flights/?controller=anonymous&foo=3" as inside ApplicationController it continues to use usual routing of the application where anonymous controller is not defined.

What is the supposed way to test such before_filters?

@dchelimsky
Member

Try response.should redirect_to(:controller => 'anonymous' :foo => 3).

See http://rubydoc.info/gems/actionpack/ActionDispatch/Assertions/ResponseAssertions:assert_redirected_to for more info (the redirect_to matcher delegates to this).

@dchelimsky dchelimsky closed this Jul 17, 2012
@Exoth
Exoth commented Jul 17, 2012

No. The problem is not with what I expect to get but with what I get. It redirects to http://test.host/flights/?controller=anonymous&foo=3 instead of http://test.host/anonymous/?foo=3 as url_for inside of application_controller method uses application routes without anonymous resource defined. "controller" method replaces application routes with anonymous resource routes only for @routes, which is used for rspec get, post methods, but not anywhere in the application itself.

@Exoth
Exoth commented Jul 17, 2012

So redirect_to inside of my spec file works right, but redirect_to inside of my application works wrong.

@Exoth
Exoth commented Jul 18, 2012

Can anyone reopen the issue? The problem is not solved.

@dchelimsky dchelimsky reopened this Jul 18, 2012
@dchelimsky
Member

@Exoth re-opened. How would you expect this to work?

@Exoth
Exoth commented Jul 18, 2012

"controller" method could also add anonymous resource to main application routing in before block and remove the resource from there in after block. This way such application code is going to function the same way with anonymous resource as with usual resources.

@dchelimsky
Member

@alindeman @justinko you guys have any thoughts on this?

@justinko
Contributor

What do you have in routes.rb to get "anonymous"?

@Exoth
Exoth commented Jul 19, 2012

Not sure if I understand the question. The "controller" method has this:

    before do
      @orig_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
      @routes.draw { resources :anonymous }
    end

    after do
      @routes, @orig_routes = @orig_routes, nil
    end

So I'm talking about the same "resources :anonymous" to be temporarily added to main application routing.

@kirikak2
kirikak2 commented Aug 9, 2012

Hello.
I met same problem.
So I solved this problem as follow.

before do
    controller.stub(:_routes).and_return(@routes)
end

I'll cross my fingers for you.

@dchelimsky
Member

@Exoth would a variation of @kirikak2's solution work for you - where we define _routes on the anonymous controller to return @routes?

@kirikak2

I wrote simple spec that have failed and fixed examples.

require 'spec_helper'

describe ApplicationController, :type => :controller do
  controller(PagesController) do
    before_filter :redirect_to_index, :only => :show

    def index
      render :text => "index page rendered."
    end

    def show
      render :text => "show page rendered."
    end
  end

  describe "GET show(failed pattern)" do
    it "redirect to index page" do
      get :show, :id => 1
      # failed! because no route match.
      response.should redirect_to(:action => :index)
    end
  end

  describe "GET show(success pattern)" do
    before do
      controller.stub(:_routes).and_return(@routes)
    end

    it "redirect to index page" do
      get :show, :id => 1
      response.should redirect_to(:action => :index)
    end
  end
end

redirect_to_index method is defined at ApplicationController.

def redirect_to_index
  redirect_to(:action => :index)
end

First example is failed, because redirect_to method use RouteSet that return ActionController#_routes.
but AnonymousController#_routes returns empty routes about "anonymous".

So I defined stub at AnonymousControler#_routes to return right route.

@alindeman
Contributor

A failing spec or cuke against rspec-rails would be helpful here. It might be reside in https://www.relishapp.com/rspec/rspec-rails/v/2-12/docs/controller-specs/anonymous-controller.

I'll try to get to this eventually, but a ready made failing test would make it much easier :)

@alindeman alindeman added a commit that closed this issue Dec 12, 2012
@alindeman alindeman Anonymous controllers define `_routes`
* To support redirection and generation of URLs from other contexts
* Fixes #573
648ba09
@alindeman alindeman closed this in 648ba09 Dec 12, 2012
@alindeman alindeman added a commit that referenced this issue Jan 7, 2013
@alindeman alindeman Changelog for #573 4d6fc91
@alindeman alindeman added a commit that referenced this issue Jan 7, 2013
@alindeman alindeman Anonymous controllers define `_routes`
* To support redirection and generation of URLs from other contexts
* Fixes #573
09793f8
@alindeman alindeman added a commit that referenced this issue Jan 7, 2013
@alindeman alindeman Changelog for #573 a273236
@adonaldson adonaldson added a commit to adonaldson/rspec-rails that referenced this issue Jan 9, 2013
@adonaldson adonaldson Alternative fix for #573
Rather than starting with a fresh routeset, we take a copy of the
existing routes.
44a6004
@n0nick
n0nick commented Feb 19, 2015

The same issue reproduces for me with rspec 3.0.4 (ruby 2.0.0p594).

I ran @kirikak2's spec and the first example fails:
https://gist.github.com/n0nick/594432eee7ccf7ee2a2c

@JonRowe
Member
JonRowe commented Feb 19, 2015

I'd suggest you try with 3.2 and then if it's still broken open a new issue with some reproducible examples given this issue is very old.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment