黄瓜场景后删除文件夹
我有 2 个模拟回形针图像上传的黄瓜场景。场景完成后,我想再次删除这些文件夹。
我有以下附件文件夹结构: :网址=> “/system/:attachment/:listing_id/:id/:style_:filename”
回形针会自动删除 :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)即使我对其进行硬编码(如上所述)它不会删除它。
有什么想法吗?
I have 2 cucumber scenarios that simulate paperclip image upload. I want to remove those folders again once scenarios are complete.
I have the following attachment folder structure:
:url => "/system/:attachment/:listing_id/:id/:style_:filename"
Paperclip automatically deletes the :id/:style_:filename folder but not the parent folder.
I have a the following in my listings controller (1 listing has many images) which works great to remove the image folder with the listing id when the listing is deleted. I need to simulate the same in Cucumber after the step is run.
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
I could a) tell cucumber to delete the :listing_id folder name after running through the scenario or b) tell cucumber to delete the listing as the final step?
I've tried adding this to my cucumber env.rb file:
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
But that causes problems because 1) I don't know how to get the @listing.id from that scenario, and 2) even when I hardcode it (as above) it doesn't remove it.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已经有点旧了,但是当我自己遇到同样的问题时,我就是这么做的:
所以我猜问题出在文件夹开头的“/”。至少对我来说,它不适用于该斜杠。
Already a bit older, but as I just ran over the same issue myself here is what I did:
So I guess the problem was your '/' at the beginning of the folder. At least for me it didn't work with that slash.
我想发表评论,但没有足够的理由(或者我认为)这样做。
确实没有理由这不起作用。您是否确认 FileUtils.rm_rf(dir) 实际上删除了测试环境中的目录?
您可以在“脚本/控制台测试”中对此进行测试。
I would leave a comment, but don't have the points (or so I would assume) to do so.
There really is no reason that this shouldn't work. Have you confirmed that FileUtils.rm_rf(dir) does in fact remove the directory in the test environment?
You can test this in 'script/console test'.
您可以连接 Cucumber 的
at_exit
来删除您喜欢的任何文件夹。我正在使用features/support/uploads_cleaner.rb
中的附加代码。从此makandra 卡转发。
You can hook into Cucumber's
at_exit
to remove any folders you like. I'm using the attached code infeatures/support/uploads_cleaner.rb
.Reposted from this makandra Card.