Skip to content

Instantly share code, notes, and snippets.

@sue445
Created June 5, 2014 14:18
Show Gist options
  • Save sue445/d840d6f68771a14a48fc to your computer and use it in GitHub Desktop.
Save sue445/d840d6f68771a14a48fc to your computer and use it in GitHub Desktop.
Example of using temporary directory at rspec
shared_context :uses_temp_dir do
around do |example|
Dir.mktmpdir("rspec-") do |dir|
@temp_dir = dir
example.run
end
end
attr_reader :temp_dir
end
describe "example of uses_temp_dir" do
include_context :uses_temp_dir
it "should create temp_dir" do
expect(Dir.exists?(temp_dir)).to be true
end
it "can create file in temp_dir" do
temp_file = "#{temp_dir}/temp.txt"
File.open(temp_file, "w") do |f|
f.write("foo")
end
expect(File.read(temp_file)).to eq "foo"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment