Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created October 16, 2010 19:22
Show Gist options
  • Save joefiorini/630176 to your computer and use it in GitHub Desktop.
Save joefiorini/630176 to your computer and use it in GitHub Desktop.
describe "routes to the articles controller" do
extend RouteSpecHelpers
scope_options controller: 'articles', blog: 'my_blog'
get '/blogs/my_blog/articles/new' => { action: 'new' }
post '/blogs/my_blog/articles' => { action: 'create' }
get '/blogs/my_blog/articles/1' => {action: 'show', id: '1' }
end
# routes to the articles controller
# should route GET /blogs/my_blog/articles/new to {:controller=>"articles", :blog => "my_blog", :action=>"new"}
# should route POST /blogs/my_blog/articles to {:controller=>"articles", :blog=>"my_blog", :action=>"create"}
# should route GET /blogs/my_blog/articles/1 to {:controller=>"articles", :blog=>"my_blog", :action=>"show", :id=>"1"}
module RouteSpecHelpers
def scope_options(options={})
before do
@routing_scope = options
end
end
[:get, :post, :put, :delete].each do |method|
define_method method do |route|
specify_route(method, route)
end
end
RSpec::Matchers.define :route_to_here do |route_options|
match_unless_raises Test::Unit::AssertionFailedError do |path|
assertion_path = { :method => path.keys.first, :path => path.values.first }
assert_recognizes(route_options, assertion_path)
end
failure_message_for_should do
rescued_exception.message
end
description do |path|
"route #{path.keys.first.upcase} #{path.values.first} to #{route_options.inspect}"
end
end
private
def specify_route(method, route)
specify do
path = route.keys.first
routing_options = route.values.first
routing_options = (@routing_scope || {}).merge(routing_options)
{ method => path }.should route_to_here(routing_options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment