Guest User

Untitled

a guest
Jun 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. module RoutingExampleGroupHelpers
  2. extend ActiveSupport::Concern
  3. extend RSpec::Matchers::DSL
  4.  
  5. module DestinationParser
  6. def parse_destination destination
  7. string_or_hash, options_hash = destination[0], destination[1]
  8. case string_or_hash
  9. when String
  10. controller, action = string_or_hash.split('#')
  11. options = options_hash || {}
  12. options.merge({ :controller => controller, :action => action })
  13. when Hash
  14. string_or_hash
  15. else
  16. raise ArgumentError.new "unexpected argument of class #{destination.class}"
  17. end
  18. end
  19. end
  20.  
  21. included do
  22. include Rails.application.routes.url_helpers
  23. end
  24.  
  25. def delete path
  26. { :method => :delete, :path => path }
  27. end
  28.  
  29. def get path
  30. { :method => :get, :path => path }
  31. end
  32.  
  33. def post path
  34. { :method => :post, :path => path }
  35. end
  36.  
  37. def put path
  38. { :method => :put, :path => path }
  39. end
  40.  
  41. matcher :map_to do |*destination|
  42. extend DestinationParser
  43.  
  44. match_unless_raises Test::Unit::AssertionFailedError do |request|
  45. @request = request
  46. @method = @request.delete :method
  47. @path = @request.delete :path
  48. @destination = parse_destination destination
  49. assert_recognizes(@destination, { :method => @method, :path => @path })
  50. end
  51.  
  52. failure_message_for_should do
  53. rescued_exception.message
  54. end
  55.  
  56. description do
  57. controller = @destination.delete(:controller)
  58. action = @destination.delete(:action)
  59. result = "map #{@method.to_s.upcase} #{@path} "
  60. result << " with #{@request.inspect} " unless @request.empty?
  61. result << "to #{controller}\##{action}"
  62. result << " with #{@destination.inspect}" unless @destination.empty?
  63. result
  64. end
  65. end
  66.  
  67. matcher :map_from do |*destination|
  68. extend DestinationParser
  69.  
  70. match_unless_raises Test::Unit::AssertionFailedError do |request|
  71. @request = request
  72. @method = @request.delete :method # ignored
  73. @path = @request.delete :path
  74. @destination = parse_destination destination
  75. assert_generates @path, @destination
  76. end
  77.  
  78. failure_message_for_should do
  79. rescued_exception.message
  80. end
  81.  
  82. description do
  83. controller = @destination.delete(:controller)
  84. action = @destination.delete(:action)
  85. result = "map #{@path} "
  86. result << " with #{@request.inspect} " unless @request.empty?
  87. result << "from #{controller}\##{action}"
  88. result << " with #{@destination.inspect}" unless @destination.empty?
  89. result
  90. end
  91. end
  92.  
  93. matcher :have_routing do |*destination|
  94. extend DestinationParser
  95.  
  96. match_unless_raises Test::Unit::AssertionFailedError do |request|
  97. @request = request
  98. @method = @request.delete :method
  99. @path = @request.delete :path
  100. @destination = parse_destination destination
  101. assert_routing({ :method => @method, :path => @path}, @destination)
  102. end
  103.  
  104. failure_message_for_should do
  105. rescued_exception.message
  106. end
  107.  
  108. description do
  109. controller = @destination.delete(:controller)
  110. action = @destination.delete(:action)
  111. result = "route #{@method.to_s.upcase} #{@path} "
  112. result << " with #{@request.inspect} " unless @request.empty?
  113. result << "as #{controller}\##{action}"
  114. result << " with #{@destination.inspect}" unless @destination.empty?
  115. result
  116. end
  117. end
  118.  
  119. matcher :be_recognized do
  120. match do |request|
  121. @method = request[:method]
  122. @path = request[:path]
  123. @result = true
  124.  
  125. # we need to do this because recognize_path() can still "recognize"
  126. # paths which aren't actually routable:
  127. #
  128. # route:
  129. # resource :issues, :except => :index
  130. #
  131. # assertion:
  132. # recognize_path('/issues', :method => :get)
  133. #
  134. # "routes" to:
  135. # {:controller => 'issues', :action => 'new'}
  136. begin
  137. assert_recognizes({}, { :method => @method, :path => @path })
  138. rescue ActionController::RoutingError => e
  139. @result = e.message
  140. rescue Test::Unit::AssertionFailedError => e
  141. # routable but we didn't supply an expected destination
  142. end
  143. @result == true
  144. end
  145.  
  146. failure_message_for_should do
  147. @result
  148. end
  149.  
  150. failure_message_for_should_not do
  151. "expected #{@method.to_s.upcase} #{@path} to not be routable, but it is"
  152. end
  153.  
  154. description do
  155. "recognize #{@method.to_s.upcase} #{@path}"
  156. end
  157. end
  158. end
Add Comment
Please, Sign In to add comment