title: Rspec: No such file or directory @ rb_sysopen -
body: <p>I'm trying to write a simple test for a ruby script that uses <code>gets</code> and I'm experiencing some weird behavior. </p>
  
  <p>When I run the test with <code>rspec</code> it passes fine, but as soon as I run <code>rspec</code> with any flags, like <code>rspec -f json</code> or <code>rspec -O rand</code> the test will fail and give me <code>No such file or directory @ rb_sysopen - -O</code>, where <code>-O</code> is whatever the flag that was run.</p>
  
  <p>Stranger still is that if I run <code>rspec</code> and specify the test file like <code>rspec spec/gets_spec.rb</code>, I get a completely different error:</p>
  
  <pre><code>     Failure/Error: expect { require_relative '../gets' }.to output("Hello, Lena!\n").to_stdout                         
         expected block to output "Hello, Lena!\n" to stdout, but output "Hello, Describe \"gets.rb\" do!\n"
                       Diff:                                                                                                              
  @@ -1,2 +1,2 @@                                                                                                         
  -Hello, Lena!                                                                                                           
  +Hello, Describe "gets.rb" do!                                                                                                                                                                                                                
  # ./spec/gets_spec.rb:14:in `block (2 levels) in &lt;top (required)&gt;' 
  </code></pre>
  
  <p>I'm having trouble figuring out the right way to write my test so that this doesn't happen but I'm not sure what part of my code needs to change.</p>
  
  <p>I'm using ruby 2.6.3</p>
  
  <p>I learned about testing input from <a href="https://stackoverflow.com/q/37329190/10481804">this question</a></p>
  
  <p>The script, <code>gets.rb</code>, looks like</p>
  
  <pre class="lang-rb prettyprint-override"><code>puts "Hello, #{gets.chomp.capitalize}!"
  </code></pre>
  
  <p>My test looks like</p>
  
  <pre class="lang-rb prettyprint-override"><code>describe "gets.rb" do
    before do
        $stdin = StringIO.new("lena")
    end
  
    after do 
        $stdin = STDIN
    end
  
    it "should output 'Hello, name!'" , points: 1 do
      allow($stdin).to receive(:gets).and_return("lena")
  
      expect { require_relative "../gets" }.to output("Hello, Lena!\n").to_stdout
    end
  end
  
  </code></pre>
  
  <p>Here's the full failure message:</p>
  
  <pre><code>Failures:
  
    1) gets.rb should output 'Hello, name!'
       Failure/Error: expect { require_relative '../gets' }.to output("Hello, Lena!\n").to_stdout                        
       Errno::ENOENT: 
  No such file or directory @ rb_sysopen - -O                                                                          
  #./gets.rb:1:in `gets'                     
  # ./gets.rb:1:in `gets' 
  # ./gets.rb:1:in `&lt;top (required)&gt;' 
  # ./spec/gets_spec.rb:14:in `require_relative' 
  # ./spec/gets_spec.rb:14:in `block (3 levels) in &lt;top (required)&gt;'
  # ./spec/gets_spec.rb:14:in `block (2 levels) in &lt;top (required)&gt;'  
  </code></pre>