Skip to content

Instantly share code, notes, and snippets.

@dnch
Created November 11, 2011 00:32
Show Gist options
  • Save dnch/1356747 to your computer and use it in GitHub Desktop.
Save dnch/1356747 to your computer and use it in GitHub Desktop.
module ResourcefulRequestHelper
# Iterates through each route defined for the current controller, yielding
# each one to allow the same set of assertions / expectations to be tested.
#
# Usage:
# ======
# describe UsersController do
# let(:user) { double('user') }
#
# it "allows all standard resourceful actions" do
# all_resourceful_requests(user.id, name: 'Changed Name') do
# response.should be_success
# end
# end
# end
#
#
# @param [int] the id of a resource to test member-based routes
# @params [Hash] optional paramaters, not passed to collection-based routes
def all_resourceful_requests(id, params = {})
controller_name = subject.class.name.underscore.gsub(/_controller$/, '')
pertinent_routes = Rails.application.routes.routes.select do |r|
r.defaults[:controller] == controller_name
end
pertinent_routes.each do |route|
# if the route refers to an instance, not a collection
if route.conditions[:path_info].names.include?('id')
self.send(route.verb.downcase, route.defaults[:action], params.merge(id: id))
else
self.send(route.verb.downcase, route.defaults[:action])
end
# yield to the example's expectations...
yield
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment