Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix anonymous controller route helpers #905

Closed
wants to merge 2 commits into from

3 participants

@thomasfedb

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.

@thomasfedb

Travis is failing with:

/home/travis/build/rspec/rspec-rails/lib/rspec/rails/matchers/relation_match_array.rb:2:
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.

@myronmarston
Owner

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

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

@thomasfedb

@myronmarston Okay, thanks.

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

@myronmarston
Owner

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

Have at it!

@thomasfedb

@myronmarston The new ContainExactly doesn't recognise ActiveRecord::Relation objects as being enumerable, for the obvious reason that ActiveRecord::Relation.new.ancestors 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.

@myronmarston
Owner

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

@myronmarston
Owner

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

@thomasfedb

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

@thomasfedb

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
b731d91
@alindeman alindeman closed this in b731d91
@alindeman
Collaborator

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.

@thomasfedb

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

@myronmarston
Owner

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.

@thomasfedb

@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.
View
2  .gitignore
@@ -15,3 +15,5 @@ bin
Gemfile-custom
.rbenv-version
.rbx
+.ruby-version
+.ruby-gemset
View
5 lib/rspec/rails/example/controller_example_group.rb
@@ -68,9 +68,8 @@ def self.name
end
metadata[:example_group][:described_class].class_eval(&body)
- 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 = ActionDispatch::Routing::RouteSet.new.tap { |r|
@@ -79,7 +78,7 @@ def self.name
end
after do
- self.routes = orig_routes
+ self.routes = @orig_routes
@orig_routes = nil
end
end
View
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(Class.new) { }
end
+ @anonymous_controller_before_each_hook = (group.hooks[:before][:each].send(:hooks) - orig_before_each_hooks).first
end
- 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('http://test.host/anonymous')
+
+ example = group.new
+ allow(example).to receive_messages(:controller => controller)
+
+ @anonymous_controller_before_each_hook.run(example)
+
+ expect(example.anonymous_url).to eq('http://test.host/anonymous')
+ end
+
+
+ it "delegates application route helpers to the controller" do
controller = double('controller')
allow(controller).to receive(:foos_url).and_return('http://test.host/foos')
example = group.new
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 = ActionDispatch::Routing::RouteSet.new
routes.draw { resources :foos }
- example.instance_variable_set(:@orig_routes, routes)
+ example.instance_variable_set(:@routes, routes)
+
+ @anonymous_controller_before_each_hook.run(example)
expect(example.foos_url).to eq('http://test.host/foos')
end
Something went wrong with that request. Please try again.