2011-01-31 110 views
0

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

我有以下附件文件夹结构: :URL => “/系统/:附接/:listing_id /:ID /:STYLE_:文件名

回形针自动删除:ID /: style_:文件名文件夹,但不是父文件夹。

我在列表中有一个以下列表控制器(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 

我可以一)告诉黄瓜删除:通过方案中运行或b)告诉黄瓜删除的上市作为最终步骤之后listing_id文件夹名称?

我已经尝试添加该到我的黄瓜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)即使我硬编码(如上),它不会删除它。

有什么想法?

回答

0

已经有点老,但正如我刚才跑过去同样的问题我这里是我做过什么:

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 

所以我想这个问题是你的“/”在开头的文件夹。至少对我来说,它并不适合那种斜线。

0

我会留下评论,但没有要点(或者我会假设)这样做。

真的没有理由,这不应该工作。你确认FileUtils.rm_rf(dir)确实在测试环境中删除目录吗?

你可以在'脚本/控制台测试'中测试它。

0

你可以挂钩到黄瓜的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