class Attestify::Mock

Simple class for mocking objects.

Public Class Methods

new(test_or_assertions = Attestify::Test.current_test) click to toggle source
# File lib/attestify/mock.rb, line 4
def initialize(test_or_assertions = Attestify::Test.current_test)
  @assertions = test_or_assertions
  @assertions = test_or_assertions.assertions if test_or_assertions.respond_to?(:assertions)
  @expectations_hash = Hash.new { |hash, key| hash[key] = [] }
  @expectations = []
  @called_expectations = []
end

Public Instance Methods

expect(name, return_value, args = [], &block) click to toggle source
# File lib/attestify/mock.rb, line 12
def expect(name, return_value, args = [], &block)
  name = name.to_sym
  expectation = Attestify::Mock::ExpectedCall.new(name, return_value, args, block)
  @expectations << expectation
  @expectations_hash[name.to_sym] << expectation
  self
end
method_missing(method, *args, &block) click to toggle source
# File lib/attestify/mock.rb, line 30
def method_missing(method, *args, &block) # rubocop:disable Style/MethodMissing
  expectation =
    if @expectations_hash[method].empty?
      UnexpectedCall.new(method, args, block)
    else
      @expectations_hash[method].shift
    end

  @called_expectations << expectation
  expectation.call(args, block)
end
respond_to_missing?(method, include_all = false) click to toggle source
Calls superclass method
# File lib/attestify/mock.rb, line 25
def respond_to_missing?(method, include_all = false)
  return true if @expectations_hash.include?(method)
  super
end
verify() click to toggle source
# File lib/attestify/mock.rb, line 20
def verify
  @called_expectations.each { |x| x.verify(@assertions) }
  @expectations.reject(&:called?).each { |x| x.verify(@assertions) }
end