PageRenderTime 49ms CodeModel.GetById 21ms app.highlight 2ms RepoModel.GetById 18ms 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
  7before(: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" }
 12end
 13
 14it "should use ${4:$(pluralize-string text)}Controller" do
 15  controller.should be_an_instance_of(${4:$(pluralize-string text)}Controller)
 16end
 17
 18describe "GET 'index'" do
 19  before(:each) do
 20    $2.should_receive(:find).with("1").and_return(@$1)
 21    @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
 22  end
 23
 24  describe "with :format => :html" do
 25    before(:each) do
 26      get :index, :$1_id => "1"
 27    end
 28    it_should_be_successful
 29    it_should_render "${3:$(pluralize-string text)}/index"
 30    it_should_be_assign :$1, :$3
 31    it_should_return_content_type :html
 32  end
 33
 34  describe "with :format => :xml" do
 35    before(:each) do
 36      @${3:$(pluralize-string text)}.should_receive(:to_xml).and_return("XML")
 37      get :index, :$1_id => "1", :format => "xml"
 38    end
 39    it_should_be_successful
 40    it_should_be_assign :$1, :$3
 41    it_should_return_content_type :xml
 42    it_should_response_body "XML"
 43  end
 44end
 45
 46describe "GET 'show'" do
 47  before(:each) do
 48    $2.should_receive(:find).with("1").and_return(@$1)
 49    @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
 50    @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
 51  end
 52
 53  describe "with :format => :html" do
 54    before(:each) do
 55      get :show, :$1_id => "1", :id => "1"
 56    end
 57    it_should_be_successful
 58    it_should_render "${3:$(pluralize-string text)}/show"
 59    it_should_be_assign :$1, :$3
 60  end
 61
 62  describe "with :format => :xml" do
 63    before(:each) do
 64      @$3.should_receive(:to_xml).and_return("XML")
 65      get :show, :$1_id => "1", :id => "1", :format => "xml"
 66    end
 67    it_should_be_successful
 68    it_should_return_content_type :xml
 69    it_should_be_assign :$1, :$3
 70    it_should_response_body "XML"
 71  end
 72end
 73
 74describe "GET 'new'" do
 75  before(:each) do
 76    $2.should_receive(:find).with("1").and_return(@$1)
 77    $4.should_receive(:new).with(:$1 => @$1).and_return(@$3)
 78    @$3.stub!(:new_record? => true)
 79    get :new, :$1_id => "1"
 80  end
 81  it_should_be_successful
 82  it_should_render '${3:$(pluralize-string text)}/new'
 83  it_should_be_assign :$1, :$3
 84end
 85
 86describe "GET 'edit'" do
 87  before(:each) do
 88    $2.should_receive(:find).with("1").and_return(@$1)
 89    @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
 90    @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
 91    get :edit, :$1_id => "1", :id => "1"
 92  end
 93  it_should_be_successful
 94  it_should_be_assign :$1, :$3
 95  it_should_render '${3:$(pluralize-string text)}/edit'
 96end
 97
 98describe "POST 'create'" do
 99  before(:each) do
100    $2.should_receive(:find).with("1").and_return(@$1)
101    $4.should_receive(:new).with(:$1 => @$1).and_return(@$3)
102    @$3.stub!(:new_record? => true)
103  end
104
105  describe "successfuly" do
106    before(:each) do
107      @$3.should_receive(:update_attributes!).with(@attrs)
108    end
109
110    describe "with :format => :html" do
111      before(:each) do
112        post :create, :$1_id => "1", :$3 => @attrs
113      end
114      it_should_be_redirect { $1_$3_url(@$1, @$3) }
115      it_should_be_assign :$1, :$3
116      it_should_flash_notice_have I18n.translate('${3:$(pluralize-string text)}.create.flash')
117    end
118
119    describe "with :format => :xml" do
120      before(:each) do
121        @$3.should_receive(:to_xml).and_return("XML")
122        post :create, :$1_id => "1", :$3 => @attrs, :format => "xml"
123      end
124      it_should_be_successful
125      it_should_return_content_type :xml
126      it_should_be_assign :$1, :$3
127      it_should_response_body "XML"
128      it_should_response_status "201 Created"
129      it_should_response_location { $1_$3_url(@$1, @$3) }
130    end
131  end
132
133  describe "failed" do
134    before(:each) do
135      raise_record_invalid_on @$3, :update_attributes!, @attrs
136    end
137
138    describe "with :format => :html" do
139      before(:each) do
140        post :create, :$1_id => "1", :$3 => @attrs
141        end
142      it_should_be_successful
143      it_should_be_assign :$1, :$3
144      it_should_render '${3:$(pluralize-string text)}/new'
145    end
146
147    describe "with :format => :xml" do
148      before(:each) do
149        @$3.errors.should_receive(:to_xml).and_return("errors")
150        post :create, :$1_id => "1", :$3 => @attrs, :format => "xml"
151      end
152      it_should_return_content_type :xml
153      it_should_be_assign :$1, :$3
154      it_should_response_status "422 Unprocessable Entity"
155      it_should_response_body "errors"
156    end
157  end
158end
159
160describe "PUT 'update'" do
161  before(:each) do
162    $2.should_receive(:find).with("1").and_return(@$1)
163    @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
164    @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
165  end
166
167  describe "successfuly" do
168    before(:each) do
169      @$3.should_receive(:update_attributes!).with(@attrs)
170    end
171
172    describe "with :format => :html" do
173      before(:each) do
174        put :update, :$1_id => "1", :id => "1", :$3 => @attrs
175      end
176      it_should_be_redirect { $1_$3_url(@$1, @$3) }
177      it_should_be_assign :$1, :$3
178      it_should_flash_notice_have I18n.translate("${3:$(pluralize-string text)}.update.flash")
179    end
180
181    describe "with :format => :xml" do
182      before(:each) do
183        put :update, :$1_id => "1", :id => "1", :$3 => @attrs, :format => "xml"
184      end
185      it_should_be_successful
186      it_should_return_content_type :xml
187      it_should_be_assign :$1, :$3
188      it_should_response_be_blank
189    end
190  end
191
192  describe "failed" do
193    before(:each) do
194      raise_record_invalid_on @$3, :update_attributes!, @attrs
195    end
196
197    describe "with :format => :html" do
198      before(:each) do
199        put :update, :$1_id => "1", :id => "1", :$3 => @attrs
200      end
201      it_should_be_successful
202      it_should_be_assign :$1, :$3
203      it_should_render '${3:$(pluralize-string text)}/edit'
204    end
205
206    describe "with :format => :xml" do
207      before(:each) do
208        @$3.errors.should_receive(:to_xml).and_return("errors")
209        put :update, :$1_id => "1", :id => "1", :$3 => @attrs, :format => "xml"
210      end
211      it_should_return_content_type :xml
212      it_should_be_assign :$1, :$3
213      it_should_response_status "422 Unprocessable Entity"
214      it_should_response_body "errors"
215    end
216  end
217end
218
219describe "DELETE 'destroy'" do
220  before(:each) do
221    $2.should_receive(:find).with("1").and_return(@$1)
222    @$1.should_receive(:${3:$(pluralize-string text)}).and_return(@${3:$(pluralize-string text)})
223    @${3:$(pluralize-string text)}.should_receive(:find).with("1").and_return(@$3)
224    @$3.should_receive(:destroy)
225  end
226
227  describe "with :format => :html" do
228    before(:each) do
229      delete :destroy, :$1_id => "1", :id => "1"
230    end
231    it_should_be_redirect { $1_${3:$(pluralize-string text)}_url(@$1) }
232    it_should_be_assign :$1, :$3
233    it_should_flash_notice_have I18n.translate("${3:$(pluralize-string text)}.destroy.flash")
234  end
235
236  describe "with :format => :xml" do
237    before(:each) do
238      delete :destroy, :$1_id => "1", :id => "1", :format => "xml"
239    end
240    it_should_be_successful
241    it_should_return_content_type :xml
242    it_should_be_assign :$1, :$3
243    it_should_response_be_blank
244  end
245end
246
247describe_routes_for :controller => "${3:$(pluralize-string text)}", :$1_id => "1" do
248  it_should_have_route_for_resources "${1:$(pluralize-string text)}/1/${3:$(pluralize-string text)}"
249end