对象销毁后,对象仍然可用。 rails 5

After the destruction of the object, the object is still available. rails 5

对象销毁后,对象仍然可用。 我在controller中有一个常规的方法,对象被销毁了。

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 返回了密钥。为什么? 升级到 rails 4 到 5 后出现此行为。

问题如下。文件删除是 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