Top Level Namespace

Defined Under Namespace

Modules: Dir, File, FileUtils, MockFS

Instance Method Summary collapse

Instance Method Details

#require(library_name) ⇒ Object

mockfs/override.rb overrides Kernel.require to allow you to include Ruby files that are in the mock file system:

require 'mockfs/override'
MockFS.file.open( 'virtual.rb', 'w' ) do |f|
  f.puts "puts 'I am a ruby program living in a virtual file'"
end
require 'virtual.rb'
require 'rexml'        # real files are still accessible


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mockfs/override.rb', line 64

def require( library_name )
	begin
		MockFS.mock = false
		super
		MockFS.mock = true
	rescue LoadError => err
		MockFS.mock = true
		file = library_name
		file += '.rb' unless library_name =~ /\.rb$/
		if File.exist? file
			contents = File.open( file ) do |f| f.gets( nil ); end
			eval contents
		else
			raise
		end
	end
end