我想使用around hook来运行我的specs,其中包含一个临时目录,并在之后清理临时目录。
describe FileManipulatingClass do
around(:each) do |example|
Dir.mktmpdir do |dir|
example.run
end
end
subject { described_class.new dir }
context "given its help file exists" do
let(:file_path) { File.join dir "help.txt"}
before(:each) do
File.open(file_path, 'w') {|io| io.write "some data" }
end
its(:help_text) { should eq("some data") }
end
end
这不起作用,因为没有为上下文设置“dir”。我怎样才能做相同的
let(:dir) { ... }
并提供一个只在around hook中可用的值?
答案 0 :(得分:3)
一种方法是在around
挂钩中设置实例变量,如下所示:
describe FileManipulatingClass do
around(:each) do |example|
Dir.mktmpdir do |dir|
@dir = dir
example.run
end
end
subject { described_class.new @dir }
context "given its help file exists" do
let(:file_path) { File.join @dir "help.txt"}
before(:each) do
File.open(file_path, 'w') {|io| io.write "some data" }
end
its(:help_text) { should eq("some data") }
end
end