Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Fix anonymous controller route helpers #905

wants to merge 2 commits into from

3 participants


Continuing on from #881.

Changed orig_routes to @orig_routes as it is referred to in method_missing on line 138. Bug introduced in 86aea05. Added a test that fails when fix is reverted.


Travis is failing with:

in `<top (required)>': uninitialized constant RSpec::Matchers::BuiltIn::MatchArray (NameError)

Which leaves me baffled, as I didn't touch that file or anything I can think would affect it.


In rspec-expectations I recently renamed that class. See rspec/rspec-expectations@6bde36e and rspec/rspec-expectations#398.

Use RSpec::Matchers::BuiltIn::ContainExactly instead.


@myronmarston Okay, thanks.

I guess that should be a separate PR however.
Would you be prepared to do so? Or shall I?


Would you be prepared to do so? Or shall I?

Have at it!


@myronmarston The new ContainExactly doesn't recognise ActiveRecord::Relation objects as being enumerable, for the obvious reason that doesn't include Enumerable.

I could either monkeypatch RSpec::Matchers::Composable and change the enumerable? method in the rspec-rails gem, or perhaps you could check for #to_a in the rspec-expectations gem.

This was introduced in rspec/rspec-expectations@ec93540.


I've got a fix in rspec/rspec-expectations#404.


I just merged that PR so you should be good to go now :). Sorry about the regression!


Just squashed my tests into the same commit for neatness, and to trigger Travis.


Travis is failing on one spec that appears unrelated, as mentioned in #906 by @alindeman.

@alindeman alindeman closed this pull request from a commit
@alindeman alindeman Revert "use closure scope locals rather than ivars (eliminates warnin…
…gs, Im not sure why the ivars are bing lost)"

This partially reverts commit 86aea05.

Thanks to @thomasfedb for the first proposed solution.
Resolves #905
@alindeman alindeman closed this in b731d91

For now, I decided simply to partially revert the commit. I'd love to have it covered by a spec, but I think we need a common way to run examples (extracted from -core?) first.


Consider using the spec attached to this issue once you're ready.


I think we need a common way to run examples (extracted from -core?) first.

rspec-support could be a good home for the supporting code. I'm working on improving rspec-mocks so that no monkey patching is needed to get it to work in that fashion.


@myronmarston I did notice that the spec was a little tricky to write.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2014
  1. @thomasfedb
Commits on Jan 4, 2014
  1. @thomasfedb

    Fix bug introduced by 86aea05 where routes helpers cannot be used whe…

    thomasfedb authored
    …n an
    anonymous controller has been defined.
This page is out of date. Refresh to see the latest.
2  .gitignore
@@ -15,3 +15,5 @@ bin
5 lib/rspec/rails/example/controller_example_group.rb
@@ -68,9 +68,8 @@ def
- orig_routes = nil
before do
- orig_routes = self.routes
+ @orig_routes = self.routes
resource_name = @controller.respond_to?(:controller_name) ?
@controller.controller_name.to_sym : :anonymous
self.routes = { |r|
@@ -79,7 +78,7 @@ def
after do
- self.routes = orig_routes
+ self.routes = @orig_routes
@orig_routes = nil
24 spec/rspec/rails/example/controller_example_group_spec.rb
@@ -41,24 +41,38 @@ module RSpec::Rails
describe "#controller" do
before do
+ orig_before_each_hooks = group.hooks[:before][:each].send(:hooks).dup
group.class_eval do
controller( { }
+ @anonymous_controller_before_each_hook = (group.hooks[:before][:each].send(:hooks) - orig_before_each_hooks).first
- it "delegates named route helpers to the underlying controller" do
+ it "delegates anonymous route helpers to the controller" do
+ controller = double('controller')
+ allow(controller).to receive(:anonymous_url).and_return('')
+ example =
+ allow(example).to receive_messages(:controller => controller)
+ expect(example.anonymous_url).to eq('')
+ end
+ it "delegates application route helpers to the controller" do
controller = double('controller')
allow(controller).to receive(:foos_url).and_return('')
example =
allow(example).to receive_messages(:controller => controller)
- # As in the routing example spec, this is pretty invasive, but not sure
- # how to do it any other way as the correct operation relies on before
- # hooks
routes =
routes.draw { resources :foos }
- example.instance_variable_set(:@orig_routes, routes)
+ example.instance_variable_set(:@routes, routes)
expect(example.foos_url).to eq('')
Something went wrong with that request. Please try again.