class EcomDev::ChefSpec::Stub::FileSystem

Public Class Methods

new() click to toggle source
# File lib/ecomdev/chefspec/stub/file_system.rb, line 10
def initialize
  @current_example = nil
  @stubs = {}
end

Public Instance Methods

after_example() click to toggle source
# File lib/ecomdev/chefspec/stub/file_system.rb, line 23
def after_example
  @current_example = nil
  @stubs.clear
end
before_example(example) click to toggle source
# File lib/ecomdev/chefspec/stub/file_system.rb, line 15
def before_example(example)
  @current_example = example
end
current_example() click to toggle source
# File lib/ecomdev/chefspec/stub/file_system.rb, line 19
def current_example
  @current_example
end
dir_glob(path, result = []) click to toggle source
# File lib/ecomdev/chefspec/stub/file_system.rb, line 59
def dir_glob(path, result = [])
  stub(Dir, :glob, true) do |stub|
    stub.with(path).and_return(result)
  end
end
file_exists(file, exists = true) click to toggle source
# File lib/ecomdev/chefspec/stub/file_system.rb, line 47
def file_exists(file, exists = true)
  stub(File, :exists?, true) do |stub|
    stub.with(file).and_return(exists)
  end
end
file_read(file, content, *additional_args) click to toggle source
# File lib/ecomdev/chefspec/stub/file_system.rb, line 53
def file_read(file, content, *additional_args)
  stub(File, :read, true) do |stub|
    stub.with(file, *additional_args).and_return(content)
  end
end
stub(klass, method, static=false) { |allowance| ... } click to toggle source

@return [RSpec::Mocks::Matchers::Receive]

# File lib/ecomdev/chefspec/stub/file_system.rb, line 29
def stub(klass, method, static=false)
  @stubs[static.to_s] ||= {}
  @stubs[static.to_s][klass.to_s] ||= []
  stub_method = static ? :allow : :allow_any_instance_of
  method = method.to_sym
  unless @stubs[static.to_s][klass.to_s].include?(method)
    @stubs[static.to_s][klass.to_s] << method
    current_example.send(stub_method, klass).to current_example.receive(method).and_call_original
  end

  allowance = current_example.receive(method)
  if block_given?
    yield allowance
  end

  current_example.send(stub_method, klass).to allowance
end