PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/ruby-mode/Ruby on Rails/scaffold/scaffold_spec.restful.nested

http://github.com/ridgetang/snippets
Unknown | 249 lines | 222 code | 27 blank | 0 comment | 0 complexity | b8b9d046b25edf6983e906c72cdcaa5c MD5 | raw file
  1. #name : generating a nested RESTful controller spec
  2. #key : restful.nested_spec
  3. #group : rails.scaffold_spec
  4. #condition : (rails/controller-spec?)
  5. # --
  6. $0
  7. before(:each) do
  8. @${1:owner} = mock_model(${2:Owner})
  9. @${3:`(singularize-string (rails/cur-res-title))`} = mock_model(${4:`(decamelize-string (singularize-string (rails/cur-res-title)))`}, :$1 => @$1)
  10. @${3:$(pluralize-string text)} = [@$3]
  11. @attrs = { "name" => "name" }
  12. end
  13. it "should use ${4:$(pluralize-string text)}Controller" do
  14. controller.should be_an_instance_of(${4:$(pluralize-string text)}Controller)
  15. end
  16. describe "GET 'index'" do
  17. before(:each) do
  18. $2.should_receive(:find).with("1").and_return(@$1)
  19. @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
  20. end
  21. describe "with :format => :html" do
  22. before(:each) do
  23. get :index, :$1_id => "1"
  24. end
  25. it_should_be_successful
  26. it_should_render "${3:$(pluralize-string text)}/index"
  27. it_should_be_assign :$1, :$3
  28. it_should_return_content_type :html
  29. end
  30. describe "with :format => :xml" do
  31. before(:each) do
  32. @${3:$(pluralize-string text)}.should_receive(:to_xml).and_return("XML")
  33. get :index, :$1_id => "1", :format => "xml"
  34. end
  35. it_should_be_successful
  36. it_should_be_assign :$1, :$3
  37. it_should_return_content_type :xml
  38. it_should_response_body "XML"
  39. end
  40. end
  41. describe "GET 'show'" do
  42. before(:each) do
  43. $2.should_receive(:find).with("1").and_return(@$1)
  44. @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
  45. @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
  46. end
  47. describe "with :format => :html" do
  48. before(:each) do
  49. get :show, :$1_id => "1", :id => "1"
  50. end
  51. it_should_be_successful
  52. it_should_render "${3:$(pluralize-string text)}/show"
  53. it_should_be_assign :$1, :$3
  54. end
  55. describe "with :format => :xml" do
  56. before(:each) do
  57. @$3.should_receive(:to_xml).and_return("XML")
  58. get :show, :$1_id => "1", :id => "1", :format => "xml"
  59. end
  60. it_should_be_successful
  61. it_should_return_content_type :xml
  62. it_should_be_assign :$1, :$3
  63. it_should_response_body "XML"
  64. end
  65. end
  66. describe "GET 'new'" do
  67. before(:each) do
  68. $2.should_receive(:find).with("1").and_return(@$1)
  69. $4.should_receive(:new).with(:$1 => @$1).and_return(@$3)
  70. @$3.stub!(:new_record? => true)
  71. get :new, :$1_id => "1"
  72. end
  73. it_should_be_successful
  74. it_should_render '${3:$(pluralize-string text)}/new'
  75. it_should_be_assign :$1, :$3
  76. end
  77. describe "GET 'edit'" do
  78. before(:each) do
  79. $2.should_receive(:find).with("1").and_return(@$1)
  80. @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
  81. @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
  82. get :edit, :$1_id => "1", :id => "1"
  83. end
  84. it_should_be_successful
  85. it_should_be_assign :$1, :$3
  86. it_should_render '${3:$(pluralize-string text)}/edit'
  87. end
  88. describe "POST 'create'" do
  89. before(:each) do
  90. $2.should_receive(:find).with("1").and_return(@$1)
  91. $4.should_receive(:new).with(:$1 => @$1).and_return(@$3)
  92. @$3.stub!(:new_record? => true)
  93. end
  94. describe "successfuly" do
  95. before(:each) do
  96. @$3.should_receive(:update_attributes!).with(@attrs)
  97. end
  98. describe "with :format => :html" do
  99. before(:each) do
  100. post :create, :$1_id => "1", :$3 => @attrs
  101. end
  102. it_should_be_redirect { $1_$3_url(@$1, @$3) }
  103. it_should_be_assign :$1, :$3
  104. it_should_flash_notice_have I18n.translate('${3:$(pluralize-string text)}.create.flash')
  105. end
  106. describe "with :format => :xml" do
  107. before(:each) do
  108. @$3.should_receive(:to_xml).and_return("XML")
  109. post :create, :$1_id => "1", :$3 => @attrs, :format => "xml"
  110. end
  111. it_should_be_successful
  112. it_should_return_content_type :xml
  113. it_should_be_assign :$1, :$3
  114. it_should_response_body "XML"
  115. it_should_response_status "201 Created"
  116. it_should_response_location { $1_$3_url(@$1, @$3) }
  117. end
  118. end
  119. describe "failed" do
  120. before(:each) do
  121. raise_record_invalid_on @$3, :update_attributes!, @attrs
  122. end
  123. describe "with :format => :html" do
  124. before(:each) do
  125. post :create, :$1_id => "1", :$3 => @attrs
  126. end
  127. it_should_be_successful
  128. it_should_be_assign :$1, :$3
  129. it_should_render '${3:$(pluralize-string text)}/new'
  130. end
  131. describe "with :format => :xml" do
  132. before(:each) do
  133. @$3.errors.should_receive(:to_xml).and_return("errors")
  134. post :create, :$1_id => "1", :$3 => @attrs, :format => "xml"
  135. end
  136. it_should_return_content_type :xml
  137. it_should_be_assign :$1, :$3
  138. it_should_response_status "422 Unprocessable Entity"
  139. it_should_response_body "errors"
  140. end
  141. end
  142. end
  143. describe "PUT 'update'" do
  144. before(:each) do
  145. $2.should_receive(:find).with("1").and_return(@$1)
  146. @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
  147. @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
  148. end
  149. describe "successfuly" do
  150. before(:each) do
  151. @$3.should_receive(:update_attributes!).with(@attrs)
  152. end
  153. describe "with :format => :html" do
  154. before(:each) do
  155. put :update, :$1_id => "1", :id => "1", :$3 => @attrs
  156. end
  157. it_should_be_redirect { $1_$3_url(@$1, @$3) }
  158. it_should_be_assign :$1, :$3
  159. it_should_flash_notice_have I18n.translate("${3:$(pluralize-string text)}.update.flash")
  160. end
  161. describe "with :format => :xml" do
  162. before(:each) do
  163. put :update, :$1_id => "1", :id => "1", :$3 => @attrs, :format => "xml"
  164. end
  165. it_should_be_successful
  166. it_should_return_content_type :xml
  167. it_should_be_assign :$1, :$3
  168. it_should_response_be_blank
  169. end
  170. end
  171. describe "failed" do
  172. before(:each) do
  173. raise_record_invalid_on @$3, :update_attributes!, @attrs
  174. end
  175. describe "with :format => :html" do
  176. before(:each) do
  177. put :update, :$1_id => "1", :id => "1", :$3 => @attrs
  178. end
  179. it_should_be_successful
  180. it_should_be_assign :$1, :$3
  181. it_should_render '${3:$(pluralize-string text)}/edit'
  182. end
  183. describe "with :format => :xml" do
  184. before(:each) do
  185. @$3.errors.should_receive(:to_xml).and_return("errors")
  186. put :update, :$1_id => "1", :id => "1", :$3 => @attrs, :format => "xml"
  187. end
  188. it_should_return_content_type :xml
  189. it_should_be_assign :$1, :$3
  190. it_should_response_status "422 Unprocessable Entity"
  191. it_should_response_body "errors"
  192. end
  193. end
  194. end
  195. describe "DELETE 'destroy'" do
  196. before(:each) do
  197. $2.should_receive(:find).with("1").and_return(@$1)
  198. @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
  199. @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
  200. @$3.should_receive(:destroy)
  201. end
  202. describe "with :format => :html" do
  203. before(:each) do
  204. delete :destroy, :$1_id => "1", :id => "1"
  205. end
  206. it_should_be_redirect { $1_${3:$(pluralize-string text)}_url(@$1) }
  207. it_should_be_assign :$1, :$3
  208. it_should_flash_notice_have I18n.translate("${3:$(pluralize-string text)}.destroy.flash")
  209. end
  210. describe "with :format => :xml" do
  211. before(:each) do
  212. delete :destroy, :$1_id => "1", :id => "1", :format => "xml"
  213. end
  214. it_should_be_successful
  215. it_should_return_content_type :xml
  216. it_should_be_assign :$1, :$3
  217. it_should_response_be_blank
  218. end
  219. end
  220. describe_routes_for :controller => "${3:$(pluralize-string text)}", :$1_id => "1" do
  221. it_should_have_route_for_resources "${1:$(pluralize-string text)}/1/${3:$(pluralize-string text)}"
  222. end