Skip to content

Conversation

mhenrixon
Copy link
Contributor

In our project there is no root ApplicationController and hence when you are trying to use it for comparison my anonymous controller tests blow up like the pull request suggests.

@JonRowe
Copy link
Member

JonRowe commented Feb 5, 2014

As a workaround you can specify your own base class:

describe "my controller anonymously" do
  controller(MyController) do
  end
end

or ask RSpec to infer the controller from the described_class e.g.

RSpec.configuration.infer_base_class_for_anonymous_controllers = true

describe MyController do
  controller do
  end
end

@mhenrixon
Copy link
Contributor Author

infere_base_class_for_anonymous_controller is true by default but even setting it so in my spec_helper does not solve the problem for me which is probably related to some other issue I noticed. I will try the other way you suggested though

@JonRowe
Copy link
Member

JonRowe commented Feb 5, 2014

Note you'll need to have correct constant name in your top level describe for it to work...

@mhenrixon
Copy link
Contributor Author

Your suggestion don't work since there is no class ::ApplicationController and you are trying to use it.

7) ApplicationController current session when signed in returns the current session
     Failure/Error: Unable to find matching line from backtrace
     NameError:
       uninitialized constant RSpec::Rails::ControllerExampleGroup::ClassMethods::ApplicationController

The only solution for me is to create a class application_controller.rb or all my specs blow up.

@mhenrixon
Copy link
Contributor Author

Check the test failures and it should be pretty obvious that whoever wrote that code never thought there could be a project where there is no ApplicationController in the root namespace.

@JonRowe
Copy link
Member

JonRowe commented Feb 5, 2014

Given that it's a Rails convention to have one it's a fairly safe assumption

@mhenrixon
Copy link
Contributor Author

But won't this be a problem for engines? All namespaced projects etc as well? I find it hard to believe I will be the only one.

@JonRowe
Copy link
Member

JonRowe commented Feb 5, 2014

shrug Most projects also have a root namespace, and engines probably don't use anonymous controllers. Personally I've not see anyone use them in the "wild".

@JonRowe
Copy link
Member

JonRowe commented Feb 5, 2014

I'm not saying we won't fix it by the way.

@JonRowe
Copy link
Member

JonRowe commented Feb 5, 2014

PR's accepted, although I'd prefer an additional test (hint use hide_const) rather than what you've done to the existing tests.

@mhenrixon
Copy link
Contributor Author

Deal! Didn't know that was what it was for

Sent from my iPhone

On 05 Feb 2014, at 01:20, Jon Rowe notifications@github.com wrote:

PR's accepted, although I'd prefer an additional test (hint use hide_const) rather than what you've done to the existing tests.


Reply to this email directly or view it on GitHub.

@billychan
Copy link
Contributor

I think it's reasonable for rspec-rails to assume there is an ApplicationController which is generated by rails new by default.

Your tests failed because you did something customized on Rails. I think this is totally acceptable. So it would also be acceptable to do something customized on test settings instead of changing rspec-rails.

May I suggest you to add such in spec_helper if there are heavy usage of anonymous controller

config.before(:each, type: :controller) do
  stub_const('::ApplicationController')
end

If only one or two files need such you can add them directly into the files.

@JonRowe
Copy link
Member

JonRowe commented Feb 5, 2014

You need to at least do stub_const('::ApplicationController',ActionController::Base) if you take that route.

@alindeman
Copy link
Contributor

Thanks! #924 addresses this, I think :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants