Created
February 3, 2020 14:11
-
-
Save j4zzcat/25adfede819b56edc26fbf02b26bb5ff to your computer and use it in GitHub Desktop.
Mock File#open and #read
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
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