Q:

File.read empty for a non empty file when testing with rspec

New to rubby and rspec i am trying to test a class that opens and write to a file.
The class name is SimpleLogger
Here is the spec that generates an error:
describe SimpleLogger do
  ...
  context 'when using a file' do
    require 'fakefs/spec_helpers'

    before(:all) do
      @path = 'my_file'
      logger = SimpleLogger.new @path
      logger.write "Hello, world!"
      logger.close
    end
    ...
    it 'we expect the file to have a valid content' do
      expect(File.read(@path)).to eq "Hello, world!\n"
    end
  end
end

The error generated is:
Failure/Error: expect(File.read(@path)).to eq "Hello, world!\n"

   expected: "Hello, world!\n"
        got: ""

   (compared using ==)

   Diff:
   @@ -1,2 +1 @@
   -Hello, world!

The file exists on my file system, and when I'm testing a simple puts Find.read("my_file") on an independant ruby file i've got the expected result.
I've tested and have the same issue without the fakefs gem
Why is it when run in a spec it doesn't work?
And beside that i fail to understand the advantage of fakefs, as it creates the file juste the same. So why fakefs is used?
And as it creates the file should i erase it within the spec?
Thanks in advance ;)

A:

From the documentation - it seems that you need to include the helpers to activate the FakeFS:

FakeFS::SpecHelpers provides a simple macro for RSpec example groups to turn FakeFS on and off.
  To use it simply require 'fakefs/spec_helpers', then include FakeFS::SpecHelpers into any
  example groups that you wish to use FakeFS in. For example:
require 'fakefs/spec_helpers'

describe "Some specs that deal with files" do
  include FakeFS::SpecHelpers
  ...
end

By default, including FakeFS::SpecHelpers will run for each example inside a describe block.
  If you want to turn on FakeFS one time only for all your examples, you will need to
  include FakeFS::SpecHelpers::All.
Alternatively, you can include FakeFS::SpecHelpers in all your example groups using RSpec's
  configuration block in your spec helper:
require 'fakefs/spec_helpers'

Spec::Runner.configure do |config|
  config.include FakeFS::SpecHelpers
end

If you do the above then use_fakefs will be available in all of your example groups.

You will also need to use before(:each) instead of before(:all) - like many unit test helpers, FakeFS adheres to unit-test isolation principles, in which side-effects of one test should not affect another's. That is why after every test, the gem 'resets' the state of its container, and clears all files from it.

