Skip to content

Instantly share code, notes, and snippets.

@j4zzcat
Created February 3, 2020 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j4zzcat/25adfede819b56edc26fbf02b26bb5ff to your computer and use it in GitHub Desktop.
Save j4zzcat/25adfede819b56edc26fbf02b26bb5ff to your computer and use it in GitHub Desktop.
Mock File#open and #read
require 'ostruct'
class SomeClass
def run( filename )
File.open filename, 'r' do | f |
puts f.read
end
end
end
RSpec.describe SomeClass do
it 'mocks open...' do
allow( File ).to receive( :open ).with( 'dummy', 'r' ).and_yield(
OpenStruct.new( { :read => 'Mock content of file...' } )
)
SomeClass.new.run 'dummy'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment