【问题标题】:Delete a folder after the cucumber scenario黄瓜场景后删除文件夹
【发布时间】:2011-01-31 13:42:53
【问题描述】:

我有 2 个模拟回形针图像上传的黄瓜场景。一旦场景完成,我想再次删除这些文件夹。

我有以下附件文件夹结构: :url => "/system/:attachment/:listing_id/:id/:style_:filename"

Paperclip 会自动删除 :id/:style_:filename 文件夹,但不会删除父文件夹。

我的列表控制器中有以下内容(1 个列表有很多图像),当列表被删除时,它可以很好地删除带有列表 id 的图像文件夹。运行该步骤后,我需要在 Cucumber 中进行相同的模拟。

def destroy 
    @listing = Listing.find(params[:id])  

    # if destroy was a success, remove the listing image folder
    if @listing.destroy 


    end 

    require 'fileutils'
    dir = Rails.root + '/system/photos/' + @listing.id.to_s()
    FileUtils.rm_rf(dir)


    respond_to do |format| 
        format.html { redirect_to(listings_url) } 
        format.xml { head :ok } 
    end 
end 

我可以 a) 告诉 cucumber 在运行完场景后删除 :listing_id 文件夹名称,或者 b) 告诉 cucumber 作为最后一步删除列表?

我尝试将此添加到我的黄瓜 env.rb 文件中:

AfterStep('@paperclip') do
      # This will only run before steps within scenarios tagged
      # with @cucumis AND @sativus.
        # delete folders that were created with paperclip during the test
        require 'fileutils'
        #@listing.id = 55
        #dir = Rails.root + '/system/photos/' + @listing.id.to_s()
        dir = Rails.root + '/system/photos/55' # NOT WORKING
        FileUtils.rm_rf(dir)
    end

但这会导致问题,因为 1) 我不知道如何从该场景中获取 @listing.id,并且 2) 即使我对其进行硬编码(如上所述)它也不会删除它。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails cucumber paperclip


    【解决方案1】:

    已经有点老了,但是我自己也遇到了同样的问题,这就是我所做的:

    dir = Rails.root + 'images/'
    dir.rmtree if dir.directory?
    
    # or the short form, if you know the directory will be there
    (Rails.root + 'images/').rmtree
    

    所以我猜问题出在文件夹开头的“/”。至少对我来说,它不适用于那个斜线。

    【讨论】:

      【解决方案2】:

      我会发表评论,但没有必要(或者我认为)这样做。

      确实没有理由不这样做。您是否确认 FileUtils.rm_rf(dir) 实际上确实删除了测试环境中的目录?

      您可以在“脚本/控制台测试”中对此进行测试。

      【讨论】:

        【解决方案3】:

        您可以连接到 Cucumber 的 at_exit 以删除您喜欢的任何文件夹。我正在使用features/support/uploads_cleaner.rb 中的附加代码。

        # Removes uploaded files when all scenarios for the current test process
        # are finished. Ready for parallel_tests, too.
        
        require 'fileutils'
        
        at_exit do
          directory_name = "#{ Rails.env }#{ ENV['TEST_ENV_NUMBER'] }"
          uploads_path = Rails.root.join('public/system', directory_name)
          FileUtils.remove_dir(uploads_path) if uploads_path.directory?
        end
        

        转自此makandra Card

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-11
          • 2012-02-22
          • 1970-01-01
          • 2019-12-15
          • 1970-01-01
          相关资源
          最近更新 更多