class TzuMock::Mocker

Public Class Methods

new(type, klass, rspec_context, method) click to toggle source
# File lib/tzu_mock/mocker.rb, line 3
def initialize(type, klass, rspec_context, method)
  @type, @klass, @rspec_context, @method = type, klass, rspec_context, method
end

Public Instance Methods

mock() click to toggle source
# File lib/tzu_mock/mocker.rb, line 7
def mock
  @rspec_context.instance_eval(&mock_proc(@klass, mock_methods, success?, @result, error_type))
  self
end
returns(result) click to toggle source
# File lib/tzu_mock/mocker.rb, line 12
def returns(result)
  @result = ResultBuilder.build(result)
  mock
  self
end

Private Instance Methods

error_type() click to toggle source
# File lib/tzu_mock/mocker.rb, line 42
def error_type
  case @type
  when :success
    nil
  when :invalid
    :validation
  when :failure
    :execution
  else
    raise ArgumentError.new('Invalid type, must be :success, :invalid, or :failure')
  end
end
mock_methods() click to toggle source
# File lib/tzu_mock/mocker.rb, line 33
def mock_methods
  return [@method] if @method
  TzuMock.configuration.stub_methods
end
mock_proc(klass, methods, success, result, type) click to toggle source

Need to pass variables in explicity to give the Proc access to them

# File lib/tzu_mock/mocker.rb, line 21
def mock_proc(klass, methods, success, result, type)
  Proc.new do
    methods.each do |method|
      allow(klass).to receive(method) do |&block|
        outcome = Tzu::Outcome.new(success, result, type)
        outcome.handle(&block) if block
        outcome
      end
    end
  end
end
success?() click to toggle source
# File lib/tzu_mock/mocker.rb, line 38
def success?
  @type == :success
end