Created
June 5, 2014 14:18
-
-
Save sue445/d840d6f68771a14a48fc to your computer and use it in GitHub Desktop.
Example of using temporary directory at rspec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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