2017-03-11 5 views
0

オブジェクトが破棄された後、オブジェクトは引き続き使用できます。 私は、オブジェクトが破壊されているコントローラ内に従来の方法を持っています。オブジェクトが破棄された後、オブジェクトは引き続き使用できます。レール5

def destroy 
    authorize @object, :update? 

    @attachment.destroy 
    redirect_to polymorphic_path(@object), notice: 'Attachment deleted!' 
end 

テストの問題。

describe AttachmentsController do 
    before do 
    create_account_and_login 
    @ticket = FactoryGirl.create(:ticket, account: @account) 
    @attachment = FactoryGirl.build(:attachment, object: @ticket) 
    end 

    #-------------------------------------------------------------------------- 
    describe '#destroy' do 
    it 'destroys the attachment' do 
     VCR.use_cassette_with_file 'save sailormoon.jpg and delete and check existance' do 
     @attachment.save! 

     expect { 
      delete :destroy, params: {id: @attachment.id, ticket_id: @ticket.id} 
     }.to change { @ticket.attachments.count }.by(-1) 

     expect(S3_BUCKET.object(@attachment.key).exists?).to be_falsey 
     end 

     expect(flash[:notice]).to be_present 
     expect(response).to redirect_to(ticket_path(@ticket)) 
    end 
    end 
end 

通常は何もありませんが、 delete :destroy, params: {id: @attachment.id, ticket_id: @ticket.id} 破壊される方法。 オブジェクトは削除され、すべて正常です。私は@attachment.destroyed? と確認し、trueを得た。 しかし、オブジェクトはまだ利用可能です。 @attachment.keyがキーを返しました。どうして? この動作は、レール4〜5にアップグレードした後に表示されます。

+2

@ attachachmentオブジェクトはデータベースから削除する必要がありますが、削除する前に作成されたメモリ内コピーがあるため、引き続き使用できます。 '@ attachment.reload'のようなことをするなら、例外が見つからないはずです。 –

答えて

0

問題は次のとおりです。ジョブの削除がS3であるため、コントローラーコールの周りにperform_enqueued_jobsを追加してください。

describe '#destroy' do 
    it 'destroys the attachment' do 
    VCR.use_cassette_with_file 'save sailormoon.jpg and delete and check existance' do 
     @attachment.save! 

     expect { 
     perform_enqueued_jobs { delete :destroy, params: {id: @attachment.id, ticket_id: @ticket.id} } 
     }.to change { @ticket.attachments.count }.by(-1) 

     expect(S3_BUCKET.object(@attachment.key).exists?).to be_falsey 
    end 

    expect(flash[:notice]).to be_present 
    expect(response).to redirect_to(ticket_path(@ticket)) 
    end 
end 
関連する問題