Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixing init.rb conflict

  • Loading branch information...
commit c474c9071b953ddcfad162a67ce20349c644fc13 2 parents c027272 + def98f6
Dan Croak authored September 14, 2008
10  lib/shoulda/controller/routing.rb
... ...
@@ -0,0 +1,10 @@
  1
+require 'shoulda'
  2
+require 'shoulda/controller/routing/macros'
  3
+
  4
+module Test # :nodoc: all
  5
+  module Unit
  6
+    class TestCase
  7
+      extend ThoughtBot::Shoulda::Controller::Routing::Macros
  8
+    end
  9
+  end
  10
+end
47  lib/shoulda/controller/routing/macros.rb
... ...
@@ -0,0 +1,47 @@
  1
+module ThoughtBot
  2
+  module Shoulda
  3
+    module Controller
  4
+      module Routing
  5
+        module Macros
  6
+          # Macro that creates a routing test. It tries to use the given HTTP
  7
+          # +method+ on the given +path+, and asserts that it routes to the
  8
+          # given +options+.
  9
+          #
  10
+          # If you don't specify a :controller, it will try to guess the controller
  11
+          # based on the current test.
  12
+          #
  13
+          # +to_param+ is called on the +options+ given.
  14
+          #
  15
+          # Examples:
  16
+          #
  17
+          #   should_route :get, '/posts', :action => :index
  18
+          #   should_route :post, '/posts', :controller => :posts, :action => :create
  19
+          #   should_route :get, '/posts/1', :action => :show, :id => 1
  20
+          #   should_route :put, '/posts/1', :action => :update, :id => "1"
  21
+          #   should_route :delete, '/posts/1', :action => :destroy, :id => 1
  22
+          #   should_route :get, '/posts/new', :action => :new
  23
+          # 
  24
+          def should_route(method, path, options)
  25
+            unless options[:controller]
  26
+              options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
  27
+            end
  28
+            options[:controller] = options[:controller].to_s
  29
+            options[:action] = options[:action].to_s
  30
+
  31
+            populated_path = path.dup
  32
+            options.each do |key, value|
  33
+              options[key] = value.to_param if value.respond_to? :to_param
  34
+              populated_path.gsub!(key.inspect, value.to_s)
  35
+            end
  36
+
  37
+            should_name = "route #{method.to_s.upcase} #{populated_path} to/from #{options.inspect}"
  38
+
  39
+            should should_name do
  40
+              assert_routing({:method => method, :path => populated_path}, options)
  41
+            end
  42
+          end
  43
+        end
  44
+      end
  45
+    end
  46
+  end
  47
+end
19  test/functional/posts_controller_test.rb
@@ -13,6 +13,25 @@ def setup
13 13
     @response   = ActionController::TestResponse.new
14 14
     @post       = Post.find(:first)
15 15
   end
  16
+  
  17
+  # autodetects the :controller
  18
+  should_route :get,    '/posts',     :action => :index
  19
+  # explicitly specify :controller
  20
+  should_route :post,   '/posts',     :controller => :posts, :action => :create
  21
+  # non-string parameter
  22
+  should_route :get,    '/posts/1',   :action => :show, :id => 1
  23
+  # string-parameter
  24
+  should_route :put,    '/posts/1',   :action => :update, :id => "1"
  25
+  should_route :delete, '/posts/1',   :action => :destroy, :id => 1
  26
+  should_route :get,    '/posts/new', :action => :new
  27
+  
  28
+  # Test the nested routes
  29
+  should_route :get,    '/users/5/posts',     :action => :index, :user_id => 5
  30
+  should_route :post,   '/users/5/posts',     :action => :create, :user_id => 5
  31
+  should_route :get,    '/users/5/posts/1',   :action => :show, :id => 1, :user_id => 5
  32
+  should_route :delete, '/users/5/posts/1',   :action => :destroy, :id => 1, :user_id => 5
  33
+  should_route :get,    '/users/5/posts/new', :action => :new, :user_id => 5
  34
+  should_route :put,    '/users/5/posts/1',   :action => :update, :id => 1, :user_id => 5
16 35
 
17 36
   context "The public" do
18 37
     setup do

0 notes on commit c474c90

Please sign in to comment.
Something went wrong with that request. Please try again.