OSDN Git Service

add story
[pettanr/pettanr.git] / spec / controllers / stories_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #ストーリー
4 describe StoriesController do
5   before do
6     Factory :admin
7     @license = Factory :license
8     @user = Factory :user_yas
9     @author = @user.author    #ユーザ作成時に連動して作成される
10     @comic = Factory :comic, :author_id => @user.author.id
11     @panel = Factory :panel, :author_id => @author.id
12   end
13   
14
15   describe '閲覧に於いて' do
16     before do
17       @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
18       Comic.stub(:show).and_return(@comic)
19       sign_in @user
20     end
21     context '事前チェックする' do
22     end
23     context 'つつがなく終わるとき' do
24     end
25     context '作家権限がないとき' do
26     end
27   end
28
29   describe '新規作成フォーム表示に於いて' do
30     before do
31       sign_in @user
32     end
33     context 'つつがなく終わるとき' do
34       it 'ステータスコード200 OKを返す' do
35         get :new
36         response.should be_success 
37       end
38       it '@storyに新規データを用意している' do
39         get :new
40         assigns(:story).should be_a_new(Story)
41       end
42       it 'コマモデルにデフォルト値補充を依頼している' do
43         Story.any_instance.should_receive(:supply_default).exactly(1)
44         get :new
45       end
46       context 'html形式' do
47         it 'newテンプレートを描画する' do
48           get :new
49           response.should render_template("new")
50         end
51       end
52       context 'js形式' do
53         it 'new.jsテンプレートを描画する' do
54           get :new, :format => :js
55           response.should render_template("new")
56         end
57       end
58     end
59     context '作家権限がないとき' do
60       before do
61         sign_out @user
62       end
63       context 'html形式' do
64         it 'ステータスコード302 Foundを返す' do
65           get :new
66           response.status.should eq 302
67         end
68         it 'サインインページへ遷移する' do
69           get :new
70           response.body.should redirect_to '/users/sign_in'
71         end
72       end
73       context 'js形式' do
74         it 'ステータスコード401 Unauthorizedを返す' do
75           get :new, :format => :js
76           response.status.should eq 401
77         end
78         it '応答メッセージにUnauthorizedを返す' do
79           get :new, :format => :js
80           response.message.should match(/Unauthorized/)
81         end
82       end
83     end
84   end
85   
86   describe '新規作成に於いて' do
87     before do
88       @attr = Factory.attributes_for(:story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id)
89       sign_in @user
90     end
91     context 'つつがなく終わるとき' do
92       it 'モデルに保存依頼する' do
93         Story.any_instance.should_receive(:store).exactly(1)
94         post :create, :story => @attr
95       end
96       it "@storyに作成されたコマを保持していて、それがDBにある" do
97         post :create, :story => @attr
98         assigns(:story).should be_a(Story)
99         assigns(:story).should be_persisted
100       end
101       context 'html形式' do
102         it 'ステータスコード302 Foundを返す' do
103           Story.any_instance.stub(:store).and_return(true)
104           post :create, :story => @attr
105           response.status.should eq 302
106         end
107         it 'コミックのストーリー表示へ遷移する' do
108 #          Story.any_instance.stub(:store).and_return(true)
109           post :create, :story => @attr
110           response.should redirect_to(:action => :show, :id => @attr[:comic_id])
111         end
112       end
113       context 'js形式' do
114         it 'ステータスコード302 Foundを返す' do
115           Story.any_instance.stub(:store).and_return(true)
116           post :create, :story => @attr, :format => :js
117           response.status.should eq 302
118         end
119         it 'コミックのストーリー表示へ遷移する' do
120 #          Story.any_instance.stub(:store).and_return(true)
121           post :create, :story => @attr, :format => :js
122           response.should redirect_to(:action => :show, :id => @attr[:comic_id])
123         end
124       end
125       context 'json形式' do
126         it 'ステータスコード200 OKを返す' do
127 #          Story.any_instance.stub(:store).and_return(true)
128           post :create, :story => @attr, :format => :json
129           response.should be_success 
130         end
131         it '作成されたコマをjsonデータで返す' do
132           post :create, :story => @attr, :format => :json
133           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
134         end
135         it 'データがアレになっている' do
136           post :create, :story => @attr, :format => :json
137           json = JSON.parse response.body
138           json["t"].should eq @story.t
139         end
140       end
141     end
142     context '作家権限がないとき' do
143       before do
144         sign_out @user
145       end
146       context 'html形式' do
147         it 'ステータスコード302 Foundを返す' do
148           post :create, :story => @attr
149           response.status.should eq 302
150         end
151         it 'サインインページへ遷移する' do
152           post :create, :story => @attr
153           response.body.should redirect_to '/users/sign_in'
154         end
155       end
156       context 'json形式' do
157         it 'ステータスコード401 Unauthorizedを返す' do
158           post :create, :story => @attr, :format => :json
159           response.status.should eq 401
160         end
161         it '応答メッセージにUnauthorizedを返す' do
162           post :create, :story => @attr, :format => :json
163           response.message.should match(/Unauthorized/)
164         end
165       end
166     end
167     context '検証、保存に失敗した' do
168       before do
169         Story.any_instance.stub(:store).and_return(false)
170       end
171       it "未保存のコマを保持している" do
172         post :create, :story => @attr
173         assigns(:story).should be_a_new(Story)
174       end
175       context 'html形式' do
176         it 'ステータスコード200 OKを返す' do
177           post :create, :story => @attr
178           response.status.should eq 200
179         end
180         it '新規ページを描画する' do
181           post :create, :story => @attr
182           response.should render_template("new")
183         end
184       end
185       context 'json形式' do
186         it 'ステータスコード422 unprocessable_entity を返す' do
187           post :create, :story => @attr, :format => :json
188           response.status.should eq 422
189         end
190         it '応答メッセージUnprocessable Entityを返す' do
191           post :create, :story => @attr, :format => :json
192           response.message.should match(/Unprocessable/)
193         end
194       end
195     end
196   end
197
198   describe '編集フォーム表示に於いて' do
199     before do
200       @story = Factory :story, :author_id => @author.id
201       sign_in @user
202       Story.stub(:show).and_return(@story)
203     end
204     context 'つつがなく終わるとき' do
205       it 'ステータスコード200 OKを返す' do
206         get :edit, :id => @story.id
207         response.should be_success 
208       end
209       it 'コマモデルに単体取得を問い合わせている' do
210         Story.should_receive(:show).exactly(1)
211         get :edit, :id => @story.id
212       end
213       it '@storyにデータを用意している' do
214         get :edit, :id => @story.id
215         assigns(:story).should eq @story
216       end
217       context 'html形式' do
218         it 'editテンプレートを描画する' do
219           get :edit, :id => @story.id
220           response.should render_template("edit")
221         end
222       end
223       context 'js形式' do
224         it 'edit.jsテンプレートを描画する' do
225           get :edit, :id => @story.id, :format => :js
226           response.should render_template("edit")
227         end
228       end
229     end
230     context '作家権限がないとき' do
231       before do
232         sign_out @user
233       end
234       context 'html形式' do
235         it 'ステータスコード302 Foundを返す' do
236           get :edit, :id => @story.id
237           response.status.should eq 302
238         end
239         it 'サインインページへ遷移する' do
240           get :edit, :id => @story.id
241           response.body.should redirect_to '/users/sign_in'
242         end
243       end
244       context 'js形式' do
245         it 'ステータスコード401 Unauthorizedを返す' do
246           get :edit, :id => @story.id, :format => :js
247           response.status.should eq 401
248         end
249         it '応答メッセージにUnauthorizedを返す' do
250           get :edit, :id => @story.id, :format => :js
251           response.message.should match(/Unauthorized/)
252         end
253       end
254     end
255   end
256
257   describe '更新に於いて' do
258     before do
259       @story = Factory :story, :author_id => @user.author.id
260       @attr = Factory.attributes_for(:story, :author_id => @author.id)
261       sign_in @user
262     end
263     context 'つつがなく終わるとき' do
264       it 'モデルに取得依頼する' do
265         Story.stub(:show).with(any_args).and_return(@story)
266         Story.should_receive(:show).exactly(1)
267         put :update, :id => @story.id, :story => @attr
268       end
269       it 'モデルに保存依頼する' do
270         Story.any_instance.should_receive(:store).exactly(1)
271         put :update, :id => @story.id, :story => @attr
272       end
273       it "@storyに作成されたストーリーを保持していて、それがDBにある" do
274         put :update, :id => @story.id, :story => @attr
275         assigns(:story).should be_a(Story)
276         assigns(:story).should be_persisted
277       end
278       context 'html形式' do
279         it 'ステータスコード302 Foundを返す' do
280           Story.any_instance.stub(:store).and_return(true)
281           put :update, :id => @story.id, :story => @attr
282           response.status.should eq 302
283         end
284         it 'コミックのストーリー表示へ遷移する' do
285 #          Story.any_instance.stub(:store).and_return(true)
286           put :update, :id => @story.id, :story => @attr
287           response.should redirect_to(:action => :show, :id => @attr[:comic_id])
288         end
289       end
290       context 'json形式' do
291         it 'ステータスコード200 OKを返す' do
292 #          Story.any_instance.stub(:store).and_return(true)
293           put :update, :id => @story.id, :story => @attr, :format => :json
294           response.should be_success 
295         end
296       end
297     end
298     context '作家権限がないとき' do
299       before do
300         sign_out @user
301       end
302       context 'html形式' do
303         it 'ステータスコード302 Foundを返す' do
304           put :update, :id => @story.id, :story => @attr
305           response.status.should eq 302
306         end
307         it 'サインインページへ遷移する' do
308           put :update, :id => @story.id, :story => @attr
309           response.body.should redirect_to '/users/sign_in'
310         end
311       end
312       context 'json形式' do
313         it 'ステータスコード401 Unauthorizedを返す' do
314           put :update, :id => @story.id, :story => @attr, :format => :json
315           response.status.should eq 401
316         end
317         it '応答メッセージにUnauthorizedを返す' do
318           put :update, :id => @story.id, :story => @attr, :format => :json
319           response.message.should match(/Unauthorized/)
320         end
321       end
322     end
323     context '検証、保存に失敗した' do
324       before do
325         Story.any_instance.stub(:store).and_return(false)
326       end
327       it "指定のコマを保持している" do
328         put :update, :id => @story.id, :story => @attr
329         assigns(:story).should eq @story
330       end
331       context 'html形式' do
332         it 'ステータスコード200 OKを返す' do
333           put :update, :id => @story.id, :story => @attr
334           response.status.should eq 200
335         end
336         it '編集ページを描画する' do
337           put :update, :id => @story.id, :story => @attr
338           response.should render_template("edit")
339         end
340       end
341       context 'json形式' do
342         it 'ステータスコード422 unprocessable_entity を返す' do
343           put :update, :id => @story.id, :story => @attr, :format => :json
344           response.status.should eq 422
345         end
346         it '応答メッセージUnprocessable Entityを返す' do
347           put :update, :id => @story.id, :story => @attr, :format => :json
348           response.message.should match(/Unprocessable/)
349         end
350       end
351     end
352   end
353
354 end