RouteMatcher.route_recognized does not catch MethodNotAllowed #75

Closed
cschiefelbein opened this Issue Feb 7, 2010 · 1 comment

Comments

Projects
None yet
2 participants
@cschiefelbein

If you have written a routing rule like this one:

  map.resources :roles, :except => [:edit, :update]

And you want to use the should_route (or a should_not_route) helpers in your tests:

  should_not_route :put, "roles/1", :controller => :roles,
    :action => :update, :id => 1

Then you have to monkey-patch RouteMatcher to catch the MethodNotAllowed exception (thrown by /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing_optimization.rb:64).

        def route_recognized?
          begin
            @context.send(:assert_routing,
                          { :method => @method, :path => @path },
                          @params)

            @negative_failure_message = "Didn't expect to #{description}"
            true
          rescue ::ActionController::RoutingError => error
            @failure_message = error.message
            false
          rescue ::ActionController::MethodNotAllowed => error
            @failure_message = error.message
            false
          rescue Test::Unit::AssertionFailedError => error
            @failure_message = error.message
            false
          end
        end

Here is the should_not_route if anyone wants it:

  def should_not_route(method, path, options={})
    unless options[:controller]
      options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
    end

    matcher = route(method, path).to(options)

    should "not #{matcher.description}" do
      assert_rejects matcher.in_context(self), self
    end
  end
@gabebw

This comment has been minimized.

Show comment Hide comment
@gabebw

gabebw Mar 30, 2012

Contributor

Hi @cschiefelbein, if you're still interested in this after 2 years (!), please open a pull request over at thoughtbot/shoulda-matchers. That's where all the "real" code is now. Thanks!

-- Gabe

Contributor

gabebw commented Mar 30, 2012

Hi @cschiefelbein, if you're still interested in this after 2 years (!), please open a pull request over at thoughtbot/shoulda-matchers. That's where all the "real" code is now. Thanks!

-- Gabe

@gabebw gabebw closed this Mar 30, 2012

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