OSDN Git Service

t30350#:fix destroy
[pettanr/pettanr.git] / spec / models / comic_spec.rb
index b0c740e..78a6f7a 100644 (file)
@@ -489,4 +489,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