在Rails/Rspec测试中,我使用的是CRUD‘In文件资源。在我的测试完成后,我希望能够撤销其中的任何更改,就像用事务撤消数据库更改一样。
RSpec中是否有一个特性,或者可能是另一个Gem,它监视文件系统的更改并可以恢复到以前的状态?或者必须手动撤消这些更改?
我目前正在运行Rails3、RSpec2和Capybara。
发布于 2011-12-17 03:32:05
我不知道有什么工具可以实现您想要做的事情,但是有一种方法可以这样做: 1.在每次测试2之后,将一个spec_helper.rb (:all)钩子添加到您想要恢复的目录结构中。在spec_helper.rb中添加前面(:each )和(:all)钩子,在目录结构上执行rm -r,然后取消-tar文件。
另一种,可能更有效的方法是使用rsync而不是tar。我相信rsync在只覆盖需要被覆盖的更改方面更聪明。
我相信这能实现你的目标。不好的是,如果测试被中止,您将不得不手动解压缩文件。
实际上,如果github上的项目还不存在的话,这听起来是个不错的主意。
发布于 2011-12-31 21:19:36
我听取了Brian对他所有观点的建议,但我认为我应该从我的解决方案中发布一些代码,以防其他人想做类似的事情。我还添加了一个自定义元数据标记的检查,以便只在我用:file符号标记一个测试组时才这样做。
spec_helper.rb
请注意,下面我备份了我的#{Rails.root}public/system/ENV/files目录(其中ENV = "test“或"develop"),因为我使用它来测试回形针功能,而这就是我的文件被存储的地方。
此外,我使用备份文件中的rm -r rysnc命令来恢复目录结构,而不是仅仅对目录结构执行--delete操作,它将删除在测试期间创建的所有文件。
RSpec.configure do |config|
# So we can tag tests with our own symbols, like we can do for ':js'
# to signal that we should backup and restore the filesystem before
config.treat_symbols_as_metadata_keys_with_true_values = true
config.before(:each) do
# If the example group has been tagged with the :file symbol then we'll backup
# the /public/system/ENV directory so we can roll it back after the test is over
if example.metadata[:file]
`rsync -a #{Rails.root}/public/system/#{Rails.env}/files/ #{Rails.root}/public/system/#{Rails.env}/files.back`
end
end
config.after(:each) do
# If the example group has been tagged with the file symbol then we'll revert
# the /public/system/ENV directory to the backup file we created before the test
if example.metadata[:file]
`rsync -a --delete #{Rails.root}/public/system/#{Rails.env}/files.back/ #{Rails.root}/public/system/#{Rails.env}/files/`
end
end
endsample_spec.rb
注意,我已经用:file符号标记了it“应该创建一个新文件”。
require 'spec_helper'
describe "Lesson Player", :js => true do
it "should create a new file", :file do
# Do something that creates a new file
...
end
endhttps://stackoverflow.com/questions/8540884
复制相似问题