Permalink
Browse files

Routing matchers are supported in controller specs in Rails 4

* Fixes #679
  • Loading branch information...
1 parent c386ba7 commit 55bf97ef2f852f157d4a0337e0b9d5120f6ab791 @alindeman alindeman committed Apr 7, 2013
@@ -25,6 +25,7 @@ def assertion_instance
assertion_modules.each do |mod|
mod.instance_methods.each do |method|
+ next if method == :method_missing
class_eval <<-EOM, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
assertion_instance.send(:#{method}, *args, &block)
@@ -11,6 +11,7 @@ module ControllerExampleGroup
include RSpec::Rails::Matchers::RedirectTo
include RSpec::Rails::Matchers::RenderTemplate
include RSpec::Rails::Matchers::RoutingMatchers
+ include RSpec::Rails::AssertionDelegator.new(ActionDispatch::Assertions::RoutingAssertions)
module ClassMethods
# @private
@@ -64,18 +65,27 @@ def self.name; "AnonymousController"; end
metadata[:example_group][:described_class].class_eval(&body)
before do
- @orig_routes, @routes = @routes, ActionDispatch::Routing::RouteSet.new
- @routes.draw { resources :anonymous }
+ @orig_routes = self.routes
+ self.routes = ActionDispatch::Routing::RouteSet.new.tap { |r|
+ r.draw { resources :anonymous }
+ }
end
after do
- @routes, @orig_routes = @orig_routes, nil
+ self.routes = @orig_routes
+ @orig_routes = nil
end
end
end
attr_reader :controller, :routes
+ # @api private
+ def routes=(routes)
+ @routes = routes
+ assertion_instance.instance_variable_set(:@routes, routes)
+ end
+
module BypassRescue
def rescue_with_handler(exception)
raise exception
@@ -107,7 +117,9 @@ def bypass_rescue
# If method is a named_route, delegates to the RouteSet associated with
# this controller.
def method_missing(method, *args, &block)
- if @orig_routes && @orig_routes.named_routes.helpers.include?(method)
+ if @routes && @routes.named_routes.helpers.include?(method)
+ controller.send(method, *args, &block)
+ elsif @orig_routes && @orig_routes.named_routes.helpers.include?(method)
controller.send(method, *args, &block)
else
super
@@ -120,7 +132,7 @@ def method_missing(method, *args, &block)
metadata[:type] = :controller
before do
- @routes = ::Rails.application.routes
+ self.routes = ::Rails.application.routes
end
around do |ex|
@@ -27,4 +27,17 @@ def things
expect(klass.new).to have_thing(:a)
expect(klass.new).not_to have_thing(:b)
end
+
+ it "does not delegate method_missing" do
+ assertions = Module.new {
+ def method_missing(method, *args)
+ end
+ }
+
+ klass = Class.new {
+ include RSpec::Rails::AssertionDelegator.new(assertions)
+ }
+
+ expect { klass.new.abc123 }.to raise_error(NoMethodError)
+ end
end

0 comments on commit 55bf97e

Please sign in to comment.