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