PageRenderTime 541ms CodeModel.GetById 526ms RepoModel.GetById 0ms app.codeStats 0ms

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

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