在破坏对象之后,该对象仍然可用。铁轨5

时间:2017-03-11 22:14:11

标签: ruby-on-rails rspec ruby-on-rails-5 rspec-rails

在销毁对象后,该对象仍然可用。 我在控制器中有一个常规方法,其中对象被销毁。

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之后出现此行为。

1 个答案:

答案 0 :(得分:0)

问题如下。 Аile删除是S3通过作业完成的,因此在控制器调用周围添加(def text1 "qwwqer qwasdfas") (defn countL [text char] (reduce 0 #(if (= %2 char) (+ %1 1) (+ %1 0)) text) ) (println (countL text1 "q"))

perform_enqueued_jobs