我有一个文件,我需要查看它是否存在,当我运行存根时,但该文件的位置在测试时是不确定的。我有一些关于我认为它可能有用的pseducode,但我觉得有些事情仍然是错误的。基本上,实际的代码运行Dir.glob(“/ opt / test - * / bin / progname”)。存在吗?看那里是否有文件。我是否需要将其分解为多行?
Dir.stubs(:glob).with("/opt/test-*/bin/progname").exists?returns(true)
答案 0 :(得分:1)
如果您发现Dir.glob
,那么您将不会测试您是否正确调用它。相反,您可以在示例设置中将文件写入测试位置:
before(:each) do
FileUtils.cp source, destination
end
并在以下后清理:
after(:each) do
FileUtils.rm destination
end
答案 1 :(得分:0)
您尝试的正确变体是: Dir.stubs(:glob).with(" / opt / test - * / bin / progname")。and_return(double(" glob",:'存在?& #39; => true)
您可以使用FakeFS
gem来模拟文件系统:
FakeFS.activate!
File.open('/opt/test-this/bin/progname', 'w') { |f| f.write('something') }