OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / spec / models / comic_spec.rb
index 5ffdb64..63fe8a1 100644 (file)
@@ -5,14 +5,15 @@ require 'spec_helper'
 describe Comic do
   before do
     @admin = FactoryGirl.create :admin
+    @demand_user = FactoryGirl.create :demand_user
     @sp = FactoryGirl.create :system_picture
     @lg = FactoryGirl.create :license_group
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     @user = FactoryGirl.create( :user_yas)
-    @author = @user.author
+    @author = FactoryGirl.create :author, :user_id => @user.id
     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
     @other_user = FactoryGirl.create( :user_yas)
-    @other_author = @other_user.author
+    @other_author = FactoryGirl.create :author, :user_id => @other_user.id
     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
   end
   
@@ -60,6 +61,30 @@ describe Comic do
     end
   end
   
+  describe '文字コード検証に於いて' do
+    before do
+      @comic = FactoryGirl.build :comic, :author_id => @author.id
+    end
+    
+    context 'titleを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @comic.title = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @comic.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'descriptionを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @comic.description = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @comic.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+  end
+  
   describe 'デフォルト値補充に於いて' do
     it 'visibleが0になっている' do
       @comic = FactoryGirl.build :comic, :visible => nil
@@ -76,18 +101,195 @@ describe Comic do
     end
   end
   
+  describe 'ロールリストからの作家取得に於いて' do
+    context 'ロールがユーザのとき' do
+      it 'ユーザから作家を取得して、それを返す' do
+        r = Comic.get_author_from_roles([@user])
+        r.should eq @author
+      end
+    end
+    context 'ロールが作家のとき' do
+      it '作家を返す' do
+        r = Comic.get_author_from_roles([@author])
+        r.should eq @author
+      end
+    end
+    context 'ロールが絵師のとき' do
+      it '絵師から作家を取得できれば、それをを返す' do
+        r = Comic.get_author_from_roles([@artist])
+        r.should eq @author
+      end
+      it '絵師から作家を取得できないときnilを返す' do
+        @artist.author_id = nil
+        @artist.save!
+        r = Comic.get_author_from_roles([@artist])
+        r.should be_nil
+      end
+    end
+    context 'ロールが管理者のとき' do
+      it 'nilを返す' do
+        r = Comic.get_author_from_roles([@admin])
+        r.should be_nil
+      end
+    end
+  end
+  
+  describe 'ロールリストからの絵師取得に於いて' do
+    context 'ロールがユーザのとき' do
+      it 'ユーザから作家を取得して、そこから絵師を取得できれば、それを返す' do
+        r = Comic.get_artist_from_roles([@user])
+        r.should eq @artist
+      end
+      it '作家から絵師を取得できないときnilを返す' do
+        @artist.author_id = nil
+        @artist.save!
+        r = Comic.get_artist_from_roles([@user])
+        r.should be_nil
+      end
+    end
+    context 'ロールが作家のとき' do
+      it 'そこから絵師を取得できれば、それを返す' do
+        r = Comic.get_artist_from_roles([@author])
+        r.should eq @artist
+      end
+      it '作家から絵師を取得できないときnilを返す' do
+        @artist.author_id = nil
+        @artist.save!
+        r = Comic.get_artist_from_roles([@author])
+        r.should be_nil
+      end
+    end
+    context 'ロールが絵師のとき' do
+      it 'それを返す' do
+        r = Comic.get_artist_from_roles([@artist])
+        r.should eq @artist
+      end
+    end
+    context 'ロールが管理者のとき' do
+      it 'nilを返す' do
+        r = Comic.get_artist_from_roles([@admin])
+        r.should be_nil
+      end
+    end
+  end
+  
   describe '所持判定に於いて' do
     before do
       @comic = FactoryGirl.build :comic, :author_id => @author.id
     end
-    it '自分のコミックならyes' do
-      @comic.own?(@author).should == true
+    context '事前チェックする' do
+      it '自身にロールリストからの作家取得を依頼している' do
+        Comic.should_receive(:get_author_from_roles).with(any_args).exactly(1)
+        r = @comic.own?([@author])
+      end
+    end
+    context 'ロール内作家が取得できるとき' do
+      before do
+      end
+      it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
+        Comic.stub(:get_author_from_roles).with(any_args).and_return(@author)
+        r = @comic.own?([@author])
+        r.should be_true
+      end
+      it 'ロール内作家のidが自身の作家idと一致しないならno' do
+        Comic.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
+        @comic.own?(@other_author).should be_false
+      end
+    end
+    context 'ロール内作家が取得できないとき' do
+      before do
+        Comic.stub(:get_author_from_roles).with(any_args).and_return(nil)
+      end
+      it 'Falseを返す' do
+        r = @comic.own?([@author])
+        r.should be_false
+      end
+    end
+  end
+  
+  describe '読者用ロールチェックに於いて' do
+    before do
+      @comic = FactoryGirl.build :comic, :author_id => @author.id
+    end
+    context 'ロールリストに作家が含まれるとき' do
+      it 'ロールリストがリストではないとき、リストにする trueを返す' do
+        r = @comic.reader_role_check(@author)
+        r.should be_true
+      end
+      it 'trueを返す' do
+        r = @comic.reader_role_check([@author])
+        r.should be_true
+      end
+    end
+    context 'ロールリストに絵師が含まれるとき' do
+      it 'trueを返す' do
+        r = @comic.reader_role_check([@artist])
+        r.should be_true
+      end
+    end
+    context 'ロールリストにユーザが含まれるとき' do
+      it 'trueを返す' do
+        r = @comic.reader_role_check([@user])
+        r.should be_true
+      end
+    end
+    context 'ロールリストに管理者が含まれるとき' do
+      it 'trueを返す' do
+        r = @comic.reader_role_check([@admin])
+        r.should be_true
+      end
+    end
+    context 'ロールリストにユーザ、管理者、作家、絵師が含まれないとき' do
+      it 'falseを返す' do
+        r = @comic.reader_role_check([nil])
+        r.should be_false
+      end
+    end
+  end
+  
+  describe '素材読者用ロールチェックに於いて' do
+    before do
+      @comic = FactoryGirl.build :comic, :author_id => @author.id
+    end
+    context 'ロールリストに作家が含まれるとき' do
+      it 'ロールリストがリストではないとき、リストにする trueを返す' do
+        r = @comic.resource_reader_role_check(@author)
+        r.should be_true
+      end
+      it 'trueを返す' do
+        r = @comic.resource_reader_role_check([@author])
+        r.should be_true
+      end
     end
-    it '他人のコミックならno' do
-      @comic.own?(@other_author).should == false
+    context 'ロールリストに絵師が含まれるとき' do
+      it 'trueを返す' do
+        r = @comic.resource_reader_role_check([@artist])
+        r.should be_true
+      end
+    end
+    context 'ロールリストにユーザが含まれるとき' do
+      it 'trueを返す' do
+        r = @comic.resource_reader_role_check([@user])
+        r.should be_true
+      end
+    end
+    context 'ロールリストに管理者が含まれるとき' do
+      it 'trueを返す' do
+        r = @comic.resource_reader_role_check([@admin])
+        r.should be_true
+      end
+    end
+    context 'ロールリストに借手が含まれるとき' do
+      it 'trueを返す' do
+        r = @comic.resource_reader_role_check([@demand_user])
+        r.should be_true
+      end
     end
-    it 'パラメータが作家でないならno' do
-      @comic.own?(nil).should == false
+    context 'ロールリストにユーザ、管理者、作家、絵師、借手が含まれないとき' do
+      it 'falseを返す' do
+        r = @comic.resource_reader_role_check([nil])
+        r.should be_false
+      end
     end
   end
   
@@ -95,58 +297,71 @@ describe Comic do
     before do
       @comic = FactoryGirl.build :comic, :author_id => @author.id
     end
-    context '検査対象がnil(ゲスト)のとき' do
-      context 'クローズドモードのとき' do
-        before do
-          MagicNumber['run_mode'] = 1
-        end
-        it '不許可を返す。' do\r
-          r = @comic.visible?(nil)
-          r.should be_false
-        end\r
-      end\r
-      context 'オープンモードのとき' do
-        before do
-          MagicNumber['run_mode'] = 0
-        end
-        it '公開コミックなら許可する' do\r
-          Comic.any_instance.stub(:visible).and_return(1)\r
-          r = @comic.visible?(nil)
-          r.should be_true
-        end\r
-        it '非公開コミックなら許可しない' do\r
-          Comic.any_instance.stub(:visible).and_return(0)\r
-          r = @comic.visible?(nil)
-          r.should be_false
-        end\r
-      end\r
-    end\r
-    context '検査対象が作家のとき' do
-      it '自分のコミックなら許可する' do\r
-        Comic.any_instance.stub(:own?).and_return(true)\r
-        Comic.any_instance.stub(:visible).and_return(0)\r
-        r = @comic.visible?(@author)
-        r.should == true
-      end\r
-      it '他人の非公開コミックなら許可しない' do\r
-        Comic.any_instance.stub(:own?).and_return(false)\r
-        Comic.any_instance.stub(:visible).and_return(0)\r
-        r = @comic.visible?(@author)
-        r.should == false
-      end\r
-      it '他人のコミックでも公開なら許可する' do\r
-        Comic.any_instance.stub(:own?).and_return(false)\r
-        Comic.any_instance.stub(:visible).and_return(1)\r
-        r = @comic.visible?(@author)
-        r.should == true
-      end\r
-    end\r
-    context '検査対象がそれ以外のとき' do
-      it '不許可を返す。' do\r
-        r = @comic.visible?(@admin)
+    context 'オープンモードのとき' do
+      before do
+        MagicNumber['run_mode'] = 0
+      end
+      it '自身にゲスト用ロールチェックを問い合わせしている' do
+        Comic.any_instance.stub(:guest_role_check).and_return(true)
+        Comic.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
+        r = @comic.visible?([@author])
+      end
+      it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
+        Comic.any_instance.stub(:guest_role_check).and_return(false)
+        r = @comic.visible?([@author])
+        r.should be_false
+      end
+    end
+    context 'クローズドモードのとき' do
+      before do
+        MagicNumber['run_mode'] = 1
+      end
+      it '自身に読者用ロールチェックを問い合わせしている' do
+        Comic.any_instance.stub(:reader_role_check).and_return(true)
+        Comic.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
+        r = @comic.visible?([@author])
+      end
+      it '読者用ロールチェックが失敗したとき、falseを返す' do
+        Comic.any_instance.stub(:reader_role_check).and_return(false)
+        r = @comic.visible?([@author])
+        r.should be_false
+      end
+    end
+    context '事前チェックする' do
+      before do
+        MagicNumber['run_mode'] = 1
+        Comic.any_instance.stub(:reader_role_check).and_return(true)
+        Comic.any_instance.stub(:own?).and_return(true)
+      end
+      it '自身に所持判定を問い合わせしている' do
+        Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
+        r = @comic.visible?([@author])
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        MagicNumber['run_mode'] = 1
+        Comic.any_instance.stub(:reader_role_check).and_return(true)
+      end
+      it '自分のコミックなら許可する' do
+        Comic.any_instance.stub(:own?).and_return(true)
+        Comic.any_instance.stub(:visible).and_return(0)
+        r = @comic.visible?([@author])
+        r.should be_true
+      end
+      it '他人の非公開コミックなら許可しない' do
+        Comic.any_instance.stub(:own?).and_return(false)
+        Comic.any_instance.stub(:visible).and_return(0)
+        r = @comic.visible?([@author])
         r.should be_false
-      end\r
-    end\r
+      end
+      it '他人のコミックでも公開なら許可する' do
+        Comic.any_instance.stub(:own?).and_return(false)
+        Comic.any_instance.stub(:visible).and_return(1)
+        r = @comic.visible?([@author])
+        r.should be_true
+      end
+    end
   end
   
   describe '一覧取得に於いて' do
@@ -178,13 +393,13 @@ describe Comic do
         Comic.page_size('1000').should eq Comic.max_page_size
       end
     end
-    context 'つつがなく終わるとき' do\r
-      it '一覧取得オプションを利用している' do\r
-        Comic.stub(:list_opt).with(any_args).and_return({})\r
-        Comic.should_receive(:list_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        Comic.stub(:list_opt).with(any_args).and_return({})
+        Comic.should_receive(:list_opt).with(any_args).exactly(1)
         r = Comic.list
-      end\r
-    end\r
+      end
+    end
     it 'リストを返す' do
       c = Comic.list
       c.should eq [@comic]
@@ -226,84 +441,19 @@ describe Comic do
         c.should eq [@comic]
       end
     end
-    context 'DBに5件あって1ページの件数を2件に変えたとして' do
-      before do
-        @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
-        @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
-        @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
-        @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
-        Comic.stub(:default_page_size).and_return(2)
-      end
-      it '件数0は全件(5件)を返す' do
-        r = Comic.list 5, 0
-        r.should have(5).items 
-      end
-    end
-  end
-  describe '一覧取得オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = Comic.list_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = Comic.list_opt[:include]
-      r.should have(2).items
-    end
-    it 'ストーリーを含んでいる' do
-      r = Comic.list_opt[:include]
-      r.has_key?(:stories).should be_true
-    end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.list_opt[:include]
-        r[:stories].has_key?(:panel).should be_true
-      end
-    it '作家を含んでいる' do
-      r = Comic.list_opt[:include]
-      r.has_key?(:author).should be_true
-    end
-  end
-  describe 'json一覧出力オプションに於いて' do
-    before do
-      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
-      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
-      @sbt = FactoryGirl.create :speech_balloon_template\r
-      @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
-      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
-      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
-    end
-    it 'ストーリーを含んでいる' do
-      r = Comic.list.to_json Comic.list_json_opt
-      j = JSON.parse r
-      i = j.first
-      i.has_key?('stories').should be_true
-    end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.list.to_json Comic.list_json_opt
-        j = JSON.parse r
-        i = j.first
-        s = i['stories'].first
-        s.has_key?('panel').should be_true
-      end
-    it '作家を含んでいる' do
-      r = Comic.list.to_json Comic.list_json_opt
-      j = JSON.parse r
-      i = j.first
-      i.has_key?('author').should be_true
-    end
   end
   
   describe '自分のコミック一覧取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
     end
-    context 'つつがなく終わるとき' do\r
-      it '一覧取得オプションを利用している' do\r
-        Comic.stub(:list_opt).with(any_args).and_return({})\r
-        Comic.should_receive(:list_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        Comic.stub(:list_opt).with(any_args).and_return({})
+        Comic.should_receive(:list_opt).with(any_args).exactly(1)
         r = Comic.mylist @author
-      end\r
-    end\r
+      end
+    end
     it 'リストを返す' do
       c = Comic.mylist @author
       c.should eq [@comic]
@@ -348,74 +498,223 @@ describe Comic do
         c.should eq [@comic]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+  end
+  
+  describe '他作家のコミック一覧取得に於いて' do
+    before do
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+      @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        Comic.stub(:list_opt).with(any_args).and_return({})
+        Comic.should_receive(:list_opt).with(any_args).exactly(1)
+        r = Comic.himlist @other_author
+      end
+    end
+    it '指定した作家のリストを返す' do
+      r = Comic.himlist @other_author
+      r.should eq [@other_comic]
+    end
+    it '時系列で並んでいる' do
+      nc = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
+      r = Comic.himlist @other_author
+      r.should eq [nc, @other_comic]
+    end
+    it '公開コミックに限る ' do
+      hidden = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
+      r = Comic.himlist @other_author
+      r.should eq [@other_comic]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
-        @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
-        @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
-        @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
-        Author.stub(:default_comic_page_size).and_return(2)\r
+        @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
+        @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
+        @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
+        @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
+      end
+      it '通常は2件を返す' do
+        c = Comic.himlist @other_author, 1, 2
+        c.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        c = Comic.himlist(@other_author, 1, 2)
+        c.should eq [@other_comic5, @other_comic4]
+      end
+      it 'page=2なら中間2件を返す' do
+        c = Comic.himlist(@other_author, 2, 2)
+        c.should eq [@other_comic3, @other_comic2]
       end
-      it '通常は全件(5件)を返す' do
-        r = Comic.mylist @author, 5, 0
-        r.should have(5).items 
+      it 'page=3なら先頭1件を返す' do
+        c = Comic.himlist(@other_author, 3, 2)
+        c.should eq [@other_comic]
       end
     end
   end
   
+  describe 'コミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.list_paginate 
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it 'コミック一覧の取得条件を利用している' do
+      Comic.stub(:list_where).with(any_args).and_return('')
+      Comic.should_receive(:list_where).with(any_args).exactly(1)
+      r = Comic.list_paginate 
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.list_paginate 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '自分のコミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.mylist_paginate @author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '自分のコミック一覧の取得条件を利用している' do
+      Comic.stub(:mylist_where).with(any_args).and_return('')
+      Comic.should_receive(:mylist_where).with(any_args).exactly(1)
+      r = Comic.mylist_paginate @author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.mylist_paginate @author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '他作家のコミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.himlist_paginate @other_author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '他作家のコミック一覧の取得条件を利用している' do
+      Comic.stub(:himlist_where).with(any_args).and_return('')
+      Comic.should_receive(:himlist_where).with(any_args).exactly(1)
+      r = Comic.himlist_paginate @other_author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.himlist_paginate @other_author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '一覧取得オプションに於いて' do
+    it '2つの項目を含んでいる' do
+      r = Comic.list_opt
+      r.should have(2).items
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.list_opt
+      r.has_key?(:stories).should be_true
+    end
+      it 'ストーリーはコマを含んでいる' do
+        r = Comic.list_opt
+        r[:stories].has_key?(:panel).should be_true
+      end
+    it '作家を含んでいる' do
+      r = Comic.list_opt
+      r.has_key?(:author).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+      @sbt = FactoryGirl.create :speech_balloon_template
+      @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.list.to_json Comic.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('stories').should be_true
+    end
+      it 'ストーリーはコマを含んでいる' do
+        r = Comic.list.to_json Comic.list_json_opt
+        j = JSON.parse r
+        i = j.first
+        s = i['stories'].first
+        s.has_key?('panel').should be_true
+      end
+    it '作家を含んでいる' do
+      r = Comic.list.to_json Comic.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('author').should be_true
+    end
+  end
+  
   describe '単体取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
     end
-    context 'つつがなく終わるとき' do\r
-      it '単体取得オプションを利用している' do\r
-        Comic.stub(:show_opt).with(any_args).and_return({})\r
-        Comic.should_receive(:show_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        Comic.stub(:show_opt).with(any_args).and_return({})
+        Comic.should_receive(:show_opt).with(any_args).exactly(1)
         r = Comic.show @comic.id, @author
-      end\r
-      it '閲覧許可を問い合わせている' do\r
-        Comic.any_instance.stub(:visible?).with(any_args).and_return(true)\r
-        Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
+      end
+      it '閲覧許可を問い合わせている' do
+        Comic.any_instance.stub(:visible?).with(any_args).and_return(true)
+        Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
         r = Comic.show @comic.id, @author
-      end\r
-    end\r
+      end
+    end
     it '指定のコミックを返す' do
       c = Comic.show @comic.id, @author
       c.should eq @comic
     end
-    context '閲覧許可が出なかったとき' do\r
-      it '403Forbidden例外を返す' do\r
-        Comic.any_instance.stub(:visible?).and_return(false)\r
-        lambda{\r
-          Comic.show @comic.id, @author\r
-        }.should raise_error(ActiveRecord::Forbidden)\r
-      end\r
-    end\r
-    context '存在しない作家を開こうとしたとき' do\r
-      it '404RecordNotFound例外を返す' do\r
-        lambda{\r
-          Comic.show 110, @author\r
-        }.should raise_error(ActiveRecord::RecordNotFound)\r
-      end\r
-    end\r
+    context '閲覧許可が出なかったとき' do
+      it '403Forbidden例外を返す' do
+        Comic.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          Comic.show @comic.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないコミックを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Comic.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
   end
   
   describe '編集取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
     end
-    context 'つつがなく終わるとき' do\r
-      it '単体取得オプションを利用している' do\r
-        Comic.stub(:show_opt).with(any_args).and_return({})\r
-        Comic.should_receive(:show_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        Comic.stub(:show_opt).with(any_args).and_return({})
+        Comic.should_receive(:show_opt).with(any_args).exactly(1)
         r = Comic.edit @comic.id, @author
-      end\r
-      it '所持判定を問い合わせている' do\r
-        Comic.any_instance.stub(:own?).with(any_args).and_return(true)\r
-        Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
+      end
+      it '所持判定を問い合わせている' do
+        Comic.any_instance.stub(:own?).with(any_args).and_return(true)
+        Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
         r = Comic.edit @comic.id, @author
-      end\r
-    end\r
+      end
+    end
     it '指定のコミックを返す' do
       Comic.any_instance.stub(:own?).and_return(true)
       c = Comic.edit @comic.id, @author.id
@@ -461,10 +760,10 @@ describe Comic do
   end
   describe 'json単体出力オプションに於いて' do
     before do
-      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
-      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
-      @sbt = FactoryGirl.create :speech_balloon_template\r
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+      @sbt = FactoryGirl.create :speech_balloon_template
       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
@@ -489,4 +788,53 @@ describe Comic do
       i.has_key?('author').should be_true
     end
   end
+  
+  describe '削除に於いて' do
+    before do
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
+      @other_comic = FactoryGirl.create :comic, :author_id => @author.id
+      @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
+    end
+    context 'つつがなく終わるとき' do
+      it '自身を削除する' do
+        lambda {
+          r = @comic.destroy_with_story
+        }.should change(Comic, :count).by(-1)
+        lambda {
+          r = Comic.find @comic.id
+        }.should raise_error
+      end
+      it '自身にリンクしているストーリーをすべて削除する' do
+        lambda {
+          r = @comic.destroy_with_story
+        }.should change(Story, :count).by(-1)
+        lambda {
+          r = Story.find @story.id
+        }.should raise_error
+      end
+      it 'Trueを返す' do
+        r = @comic.destroy_with_story
+        r.should be_true
+      end
+    end
+    context '削除に失敗したとき' do
+      before do
+        Story.any_instance.stub(:destroy).with(any_args).and_return(false)
+      end
+      it 'Falseを返す' do
+        r = @comic.destroy_with_story
+        r.should be_false
+      end
+      it 'ロールバックしている' do
+        lambda {
+          r = @comic.destroy_with_story
+        }.should_not change(Comic, :count)
+        lambda {
+          r = @comic.destroy_with_story
+        }.should_not change(Story, :count)
+      end
+    end
+  end
 end