Skip to content

Instantly share code, notes, and snippets.

@jch
Created October 22, 2011 18:26
Show Gist options
  • Save jch/1306317 to your computer and use it in GitHub Desktop.
Save jch/1306317 to your computer and use it in GitHub Desktop.
testing route constraints
# I was upgrading an old rails 2 app to rails 3 and noticed that
# http://rails.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html
# was deprecated. I was using this in the controllers to verify that an action had to be triggered
# by an xhr request
# I am unable to test it from a functional test b/c it doesn't look like functional tests
# go through routes. However, it works fine in the app. I could put this in an integration test,
# but I'm wonder if it makes more sense to have some way of testing the route constraint in
# a local way.
class XhrConstraint
def matches?(request)
request.xhr?
end
end
Beerpad::Application.routes.draw do
resources :beers do
collection do
get :autocomplete, :constraints => XhrConstraint.new
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment