OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[pettanr/pettanr.git] / spec / models / comic_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #コミック
4
5 describe Comic do
6   before do
7     @admin = FactoryGirl.create :admin
8     @demand_user = FactoryGirl.create :demand_user
9     @sp = FactoryGirl.create :system_picture
10     @lg = FactoryGirl.create :license_group
11     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
12     @user = FactoryGirl.create( :user_yas)
13     @author = FactoryGirl.create :author, :user_id => @user.id
14     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
15     @other_user = FactoryGirl.create( :user_yas)
16     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
17     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
18   end
19   
20   describe '検証に於いて' do
21     before do
22       @comic = FactoryGirl.build :comic, :author_id => @author.id
23     end
24     
25     context 'オーソドックスなデータのとき' do
26       it '下限データが通る' do
27         @comic.title = 'a'
28         @comic.visible = 0
29         @comic.should be_valid
30       end
31       it '上限データが通る' do
32         @comic.title = 'a'*100
33         @comic.visible = 1
34         @comic.should be_valid
35       end
36     end
37     
38     context 'titleを検証するとき' do
39       it 'nullなら失敗する' do
40         @comic.title = nil
41         @comic.should_not be_valid
42       end
43       it '100文字以上なら失敗する' do
44         @comic.title = 'a'*101
45         @comic.should_not be_valid
46       end
47     end
48     context 'visibleを検証するとき' do
49       it 'nullなら失敗する' do
50         @comic.visible = nil
51         @comic.should_not be_valid
52       end
53       it '負なら失敗する' do
54         @comic.visible = -1
55         @comic.should_not be_valid
56       end
57       it '2以上なら失敗する' do
58         @comic.visible = 2
59         @comic.should_not be_valid
60       end
61     end
62   end
63   
64   describe 'デフォルト値補充に於いて' do
65     it 'visibleが0になっている' do
66       @comic = FactoryGirl.build :comic, :visible => nil
67       @comic.supply_default
68       @comic.visible.should eq 0
69     end
70   end
71   
72   describe '上書き補充に於いて' do
73     it '作家idが設定されている' do
74       @comic = FactoryGirl.build :comic, :author_id => nil
75       @comic.overwrite @author
76       @comic.author_id.should eq @author.id
77     end
78   end
79   
80   describe 'ロールリストからの作家取得に於いて' do
81     context 'ロールがユーザのとき' do
82       it 'ユーザから作家を取得して、それを返す' do
83         r = Comic.get_author_from_roles([@user])
84         r.should eq @author
85       end
86     end
87     context 'ロールが作家のとき' do
88       it '作家を返す' do
89         r = Comic.get_author_from_roles([@author])
90         r.should eq @author
91       end
92     end
93     context 'ロールが絵師のとき' do
94       it '絵師から作家を取得できれば、それをを返す' do
95         r = Comic.get_author_from_roles([@artist])
96         r.should eq @author
97       end
98       it '絵師から作家を取得できないときnilを返す' do
99         @artist.author_id = nil
100         @artist.save!
101         r = Comic.get_author_from_roles([@artist])
102         r.should be_nil
103       end
104     end
105     context 'ロールが管理者のとき' do
106       it 'nilを返す' do
107         r = Comic.get_author_from_roles([@admin])
108         r.should be_nil
109       end
110     end
111   end
112   
113   describe 'ロールリストからの絵師取得に於いて' do
114     context 'ロールがユーザのとき' do
115       it 'ユーザから作家を取得して、そこから絵師を取得できれば、それを返す' do
116         r = Comic.get_artist_from_roles([@user])
117         r.should eq @artist
118       end
119       it '作家から絵師を取得できないときnilを返す' do
120         @artist.author_id = nil
121         @artist.save!
122         r = Comic.get_artist_from_roles([@user])
123         r.should be_nil
124       end
125     end
126     context 'ロールが作家のとき' do
127       it 'そこから絵師を取得できれば、それを返す' do
128         r = Comic.get_artist_from_roles([@author])
129         r.should eq @artist
130       end
131       it '作家から絵師を取得できないときnilを返す' do
132         @artist.author_id = nil
133         @artist.save!
134         r = Comic.get_artist_from_roles([@author])
135         r.should be_nil
136       end
137     end
138     context 'ロールが絵師のとき' do
139       it 'それを返す' do
140         r = Comic.get_artist_from_roles([@artist])
141         r.should eq @artist
142       end
143     end
144     context 'ロールが管理者のとき' do
145       it 'nilを返す' do
146         r = Comic.get_artist_from_roles([@admin])
147         r.should be_nil
148       end
149     end
150   end
151   
152   describe '所持判定に於いて' do
153     before do
154       @comic = FactoryGirl.build :comic, :author_id => @author.id
155     end
156     context '事前チェックする' do
157       it '自身にロールリストからの作家取得を依頼している' do
158         Comic.should_receive(:get_author_from_roles).with(any_args).exactly(1)
159         r = @comic.own?([@author])
160       end
161     end
162     context 'ロール内作家が取得できるとき' do
163       before do
164       end
165       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
166         Comic.stub(:get_author_from_roles).with(any_args).and_return(@author)
167         r = @comic.own?([@author])
168         r.should be_true
169       end
170       it 'ロール内作家のidが自身の作家idと一致しないならno' do
171         Comic.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
172         @comic.own?(@other_author).should be_false
173       end
174     end
175     context 'ロール内作家が取得できないとき' do
176       before do
177         Comic.stub(:get_author_from_roles).with(any_args).and_return(nil)
178       end
179       it 'Falseを返す' do
180         r = @comic.own?([@author])
181         r.should be_false
182       end
183     end
184   end
185   
186   describe '読者用ロールチェックに於いて' do
187     before do
188       @comic = FactoryGirl.build :comic, :author_id => @author.id
189     end
190     context 'ロールリストに作家が含まれるとき' do
191       it 'ロールリストがリストではないとき、リストにする trueを返す' do
192         r = @comic.reader_role_check(@author)
193         r.should be_true
194       end
195       it 'trueを返す' do
196         r = @comic.reader_role_check([@author])
197         r.should be_true
198       end
199     end
200     context 'ロールリストに絵師が含まれるとき' do
201       it 'trueを返す' do
202         r = @comic.reader_role_check([@artist])
203         r.should be_true
204       end
205     end
206     context 'ロールリストにユーザが含まれるとき' do
207       it 'trueを返す' do
208         r = @comic.reader_role_check([@user])
209         r.should be_true
210       end
211     end
212     context 'ロールリストに管理者が含まれるとき' do
213       it 'trueを返す' do
214         r = @comic.reader_role_check([@admin])
215         r.should be_true
216       end
217     end
218     context 'ロールリストにユーザ、管理者、作家、絵師が含まれないとき' do
219       it 'falseを返す' do
220         r = @comic.reader_role_check([nil])
221         r.should be_false
222       end
223     end
224   end
225   
226   describe '素材読者用ロールチェックに於いて' do
227     before do
228       @comic = FactoryGirl.build :comic, :author_id => @author.id
229     end
230     context 'ロールリストに作家が含まれるとき' do
231       it 'ロールリストがリストではないとき、リストにする trueを返す' do
232         r = @comic.resource_reader_role_check(@author)
233         r.should be_true
234       end
235       it 'trueを返す' do
236         r = @comic.resource_reader_role_check([@author])
237         r.should be_true
238       end
239     end
240     context 'ロールリストに絵師が含まれるとき' do
241       it 'trueを返す' do
242         r = @comic.resource_reader_role_check([@artist])
243         r.should be_true
244       end
245     end
246     context 'ロールリストにユーザが含まれるとき' do
247       it 'trueを返す' do
248         r = @comic.resource_reader_role_check([@user])
249         r.should be_true
250       end
251     end
252     context 'ロールリストに管理者が含まれるとき' do
253       it 'trueを返す' do
254         r = @comic.resource_reader_role_check([@admin])
255         r.should be_true
256       end
257     end
258     context 'ロールリストに借手が含まれるとき' do
259       it 'trueを返す' do
260         r = @comic.resource_reader_role_check([@demand_user])
261         r.should be_true
262       end
263     end
264     context 'ロールリストにユーザ、管理者、作家、絵師、借手が含まれないとき' do
265       it 'falseを返す' do
266         r = @comic.resource_reader_role_check([nil])
267         r.should be_false
268       end
269     end
270   end
271   
272   describe '閲覧許可に於いて' do
273     before do
274       @comic = FactoryGirl.build :comic, :author_id => @author.id
275     end
276     context 'オープンモードのとき' do
277       before do
278         MagicNumber['run_mode'] = 0
279       end
280       it '自身にゲスト用ロールチェックを問い合わせしている' do
281         Comic.any_instance.stub(:guest_role_check).and_return(true)
282         Comic.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
283         r = @comic.visible?([@author])
284       end
285       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
286         Comic.any_instance.stub(:guest_role_check).and_return(false)
287         r = @comic.visible?([@author])
288         r.should be_false
289       end
290     end
291     context 'クローズドモードのとき' do
292       before do
293         MagicNumber['run_mode'] = 1
294       end
295       it '自身に読者用ロールチェックを問い合わせしている' do
296         Comic.any_instance.stub(:reader_role_check).and_return(true)
297         Comic.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
298         r = @comic.visible?([@author])
299       end
300       it '読者用ロールチェックが失敗したとき、falseを返す' do
301         Comic.any_instance.stub(:reader_role_check).and_return(false)
302         r = @comic.visible?([@author])
303         r.should be_false
304       end
305     end
306     context '事前チェックする' do
307       before do
308         MagicNumber['run_mode'] = 1
309         Comic.any_instance.stub(:reader_role_check).and_return(true)
310         Comic.any_instance.stub(:own?).and_return(true)
311       end
312       it '自身に所持判定を問い合わせしている' do
313         Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
314         r = @comic.visible?([@author])
315       end
316     end
317     context 'つつがなく終わるとき' do
318       before do
319         MagicNumber['run_mode'] = 1
320         Comic.any_instance.stub(:reader_role_check).and_return(true)
321       end
322       it '自分のコミックなら許可する' do
323         Comic.any_instance.stub(:own?).and_return(true)
324         Comic.any_instance.stub(:visible).and_return(0)
325         r = @comic.visible?([@author])
326         r.should be_true
327       end
328       it '他人の非公開コミックなら許可しない' do
329         Comic.any_instance.stub(:own?).and_return(false)
330         Comic.any_instance.stub(:visible).and_return(0)
331         r = @comic.visible?([@author])
332         r.should be_false
333       end
334       it '他人のコミックでも公開なら許可する' do
335         Comic.any_instance.stub(:own?).and_return(false)
336         Comic.any_instance.stub(:visible).and_return(1)
337         r = @comic.visible?([@author])
338         r.should be_true
339       end
340     end
341   end
342   
343   describe '一覧取得に於いて' do
344     before do
345       @comic = FactoryGirl.create :comic, :author_id => @author.id
346     end
347     context 'page補正について' do
348       it '文字列から数値に変換される' do
349         Comic.page('8').should eq 8
350       end
351       it 'nilの場合は1になる' do
352         Comic.page().should eq 1
353       end
354       it '0以下の場合は1になる' do
355         Comic.page('0').should eq 1
356       end
357     end
358     context 'page_size補正について' do
359       it '文字列から数値に変換される' do
360         Comic.page_size('7').should eq 7
361       end
362       it 'nilの場合はComic.default_page_sizeになる' do
363         Comic.page_size().should eq Comic.default_page_size
364       end
365       it '0以下の場合はComic.default_page_sizeになる' do
366         Comic.page_size('0').should eq Comic.default_page_size
367       end
368       it 'Comic.max_page_sizeを超えた場合はComic.max_page_sizeになる' do
369         Comic.page_size('1000').should eq Comic.max_page_size
370       end
371     end
372     context 'つつがなく終わるとき' do
373       it '一覧取得オプションを利用している' do
374         Comic.stub(:list_opt).with(any_args).and_return({})
375         Comic.should_receive(:list_opt).with(any_args).exactly(1)
376         r = Comic.list
377       end
378     end
379     it 'リストを返す' do
380       c = Comic.list
381       c.should eq [@comic]
382     end
383     it '非公開コミックは(自分のコミックであっても)含んでいない' do
384       FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
385       c = Comic.list
386       c.should eq [@comic]
387     end
388     it '時系列で並んでいる' do
389       #公開コミックは(他人のコミックであっても)含んでいる
390       v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
391       c = Comic.list
392       c.should eq [v, @comic]
393     end
394     context 'DBに5件あって1ページの件数を2件に変えたとして' do
395       before do
396         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
397         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
398         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
399         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
400         Comic.stub(:default_page_size).and_return(2)
401       end
402       it '通常は2件を返す' do
403         c = Comic.list
404         c.should have(2).items 
405       end
406       it 'page=1なら末尾2件を返す' do
407         #時系列で並んでいる
408         c = Comic.list(1)
409         c.should eq [@comic5, @comic4]
410       end
411       it 'page=2なら中間2件を返す' do
412         c = Comic.list(2)
413         c.should eq [@comic3, @comic2]
414       end
415       it 'page=3なら先頭1件を返す' do
416         c = Comic.list(3)
417         c.should eq [@comic]
418       end
419     end
420     context 'DBに5件あって1ページの件数を2件に変えたとして' do
421       before do
422         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
423         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
424         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
425         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
426         Comic.stub(:default_page_size).and_return(2)
427       end
428       it '件数0は全件(5件)を返す' do
429         r = Comic.list 5, 0
430         r.should have(5).items 
431       end
432     end
433   end
434   describe '一覧取得オプションに於いて' do
435     it 'includeキーを含んでいる' do
436       r = Comic.list_opt
437       r.has_key?(:include).should be_true
438     end
439     it '2つの項目を含んでいる' do
440       r = Comic.list_opt[:include]
441       r.should have(2).items
442     end
443     it 'ストーリーを含んでいる' do
444       r = Comic.list_opt[:include]
445       r.has_key?(:stories).should be_true
446     end
447       it 'ストーリーはコマを含んでいる' do
448         r = Comic.list_opt[:include]
449         r[:stories].has_key?(:panel).should be_true
450       end
451     it '作家を含んでいる' do
452       r = Comic.list_opt[:include]
453       r.has_key?(:author).should be_true
454     end
455   end
456   describe 'json一覧出力オプションに於いて' do
457     before do
458       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
459       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
460       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
461       @sbt = FactoryGirl.create :speech_balloon_template
462       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
463       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
464       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
465     end
466     it 'ストーリーを含んでいる' do
467       r = Comic.list.to_json Comic.list_json_opt
468       j = JSON.parse r
469       i = j.first
470       i.has_key?('stories').should be_true
471     end
472       it 'ストーリーはコマを含んでいる' do
473         r = Comic.list.to_json Comic.list_json_opt
474         j = JSON.parse r
475         i = j.first
476         s = i['stories'].first
477         s.has_key?('panel').should be_true
478       end
479     it '作家を含んでいる' do
480       r = Comic.list.to_json Comic.list_json_opt
481       j = JSON.parse r
482       i = j.first
483       i.has_key?('author').should be_true
484     end
485   end
486   
487   describe '自分のコミック一覧取得に於いて' do
488     before do
489       @comic = FactoryGirl.create :comic, :author_id => @author.id
490     end
491     context 'つつがなく終わるとき' do
492       it '一覧取得オプションを利用している' do
493         Comic.stub(:list_opt).with(any_args).and_return({})
494         Comic.should_receive(:list_opt).with(any_args).exactly(1)
495         r = Comic.mylist @author
496       end
497     end
498     it 'リストを返す' do
499       c = Comic.mylist @author
500       c.should eq [@comic]
501     end
502     it '時系列で並んでいる' do
503       nc = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
504       cl = Comic.mylist @author
505       cl.should eq [nc, @comic]
506     end
507     it '他人のコミックは公開でも含まない' do
508       nc = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
509       cl = Comic.mylist @author
510       cl.should eq [@comic]
511     end
512     it '自分のコミックは非公開でも含んでいる' do
513       nc = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
514       cl = Comic.mylist @author
515       cl.should eq [nc, @comic]
516     end
517     context 'DBに5件あって1ページの件数を2件に変えたとして' do
518       before do
519         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
520         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
521         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
522         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
523       end
524       it '通常は2件を返す' do
525         c = Comic.mylist @author, 1, 2
526         c.should have(2).items 
527       end
528       it 'page=1なら末尾2件を返す' do
529         #時系列で並んでいる
530         c = Comic.mylist(@author, 1, 2)
531         c.should eq [@comic5, @comic4]
532       end
533       it 'page=2なら中間2件を返す' do
534         c = Comic.mylist(@author, 2, 2)
535         c.should eq [@comic3, @comic2]
536       end
537       it 'page=3なら先頭1件を返す' do
538         c = Comic.mylist(@author, 3, 2)
539         c.should eq [@comic]
540       end
541     end
542     context 'DBに5件あって1ページの件数を0件に変えたとして' do
543       before do
544         @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
545         @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
546         @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
547         @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
548         Author.stub(:default_comic_page_size).and_return(2)
549       end
550       it '通常は全件(5件)を返す' do
551         r = Comic.mylist @author, 5, 0
552         r.should have(5).items 
553       end
554     end
555   end
556   
557   describe '単体取得に於いて' do
558     before do
559       @comic = FactoryGirl.create :comic, :author_id => @author.id
560     end
561     context 'つつがなく終わるとき' do
562       it '単体取得オプションを利用している' do
563         Comic.stub(:show_opt).with(any_args).and_return({})
564         Comic.should_receive(:show_opt).with(any_args).exactly(1)
565         r = Comic.show @comic.id, @author
566       end
567       it '閲覧許可を問い合わせている' do
568         Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
569         Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
570         r = Comic.show @comic.id, @author
571       end
572     end
573     it '指定のコミックを返す' do
574       c = Comic.show @comic.id, @author
575       c.should eq @comic
576     end
577     context '閲覧許可が出なかったとき' do
578       it '403Forbidden例外を返す' do
579         Comic.any_instance.stub(:visible?).and_return(false)
580         lambda{
581           Comic.show @comic.id, @author
582         }.should raise_error(ActiveRecord::Forbidden)
583       end
584     end
585     context '存在しないコミックを開こうとしたとき' do
586       it '404RecordNotFound例外を返す' do
587         lambda{
588           Comic.show 110, @author
589         }.should raise_error(ActiveRecord::RecordNotFound)
590       end
591     end
592   end
593   
594   describe '編集取得に於いて' do
595     before do
596       @comic = FactoryGirl.create :comic, :author_id => @author.id
597     end
598     context 'つつがなく終わるとき' do
599       it '単体取得オプションを利用している' do
600         Comic.stub(:show_opt).with(any_args).and_return({})
601         Comic.should_receive(:show_opt).with(any_args).exactly(1)
602         r = Comic.edit @comic.id, @author
603       end
604       it '所持判定を問い合わせている' do
605         Comic.any_instance.stub(:own?).with(any_args).and_return(true)
606         Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
607         r = Comic.edit @comic.id, @author
608       end
609     end
610     it '指定のコミックを返す' do
611       Comic.any_instance.stub(:own?).and_return(true)
612       c = Comic.edit @comic.id, @author.id
613       c.should eq @comic
614     end
615     context '他人のコミックを開こうとしたとき' do
616       it '403Forbidden例外を返す' do
617         Comic.any_instance.stub(:own?).and_return(false)
618         lambda{
619           Comic.edit @comic.id, @author
620         }.should raise_error(ActiveRecord::Forbidden)
621       end
622     end
623     context '存在しないコミックを開こうとしたとき' do
624       it '404RecordNotFound例外を返す' do
625         lambda{
626           Comic.edit 110, @author
627         }.should raise_error(ActiveRecord::RecordNotFound)
628       end
629     end
630   end
631   describe '単体取得オプションに於いて' do
632     it 'includeキーを含んでいる' do
633       r = Comic.show_opt
634       r.has_key?(:include).should be_true
635     end
636     it '2つの項目を含んでいる' do
637       r = Comic.show_opt[:include]
638       r.should have(2).items
639     end
640     it '作家を含んでいる' do
641       r = Comic.show_opt[:include]
642       r.has_key?(:author).should be_true
643     end
644     it 'ストーリーを含んでいる' do
645       r = Comic.show_opt[:include]
646       r.has_key?(:stories).should be_true
647     end
648       it 'ストーリーはコマを含んでいる' do
649         r = Comic.show_opt[:include]
650         r[:stories].has_key?(:panel).should be_true
651       end
652   end
653   describe 'json単体出力オプションに於いて' do
654     before do
655       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
656       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
657       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
658       @sbt = FactoryGirl.create :speech_balloon_template
659       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
660       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
661       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
662     end
663     it 'ストーリーを含んでいる' do
664       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
665       j = JSON.parse r
666       i = j
667       i.has_key?('stories').should be_true
668     end
669       it 'ストーリーはコマを含んでいる' do
670         r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
671         j = JSON.parse r
672         i = j
673         s = i['stories'].first
674         s.has_key?('panel').should be_true
675       end
676     it '作家を含んでいる' do
677       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
678       j = JSON.parse r
679       i = j
680       i.has_key?('author').should be_true
681     end
682   end
683   
684   describe '削除に於いて' do
685     before do
686       @comic = FactoryGirl.create :comic, :author_id => @author.id
687       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
688       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
689       @other_comic = FactoryGirl.create :comic, :author_id => @author.id
690       @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
691     end
692     context 'つつがなく終わるとき' do
693       it '自身を削除する' do
694         lambda {
695           r = @comic.destroy_with_story
696         }.should change(Comic, :count).by(-1)
697         lambda {
698           r = Comic.find @comic.id
699         }.should raise_error
700       end
701       it '自身にリンクしているストーリーをすべて削除する' do
702         lambda {
703           r = @comic.destroy_with_story
704         }.should change(Story, :count).by(-1)
705         lambda {
706           r = Story.find @story.id
707         }.should raise_error
708       end
709       it 'Trueを返す' do
710         r = @comic.destroy_with_story
711         r.should be_true
712       end
713     end
714     context '削除に失敗したとき' do
715       before do
716         Story.any_instance.stub(:destroy).with(any_args).and_return(false)
717       end
718       it 'Falseを返す' do
719         r = @comic.destroy_with_story
720         r.should be_false
721       end
722       it 'ロールバックしている' do
723         lambda {
724           r = @comic.destroy_with_story
725         }.should_not change(Comic, :count)
726         lambda {
727           r = @comic.destroy_with_story
728         }.should_not change(Story, :count)
729       end
730     end
731   end
732 end