Class: Grift::MockMethod::MockExecutions

Inherits:
Object
  • Object
show all
Defined in:
lib/grift/mock_method/mock_executions.rb,
lib/grift/mock_method/mock_executions/mock_arguments.rb

Overview

An Array wrapper that tracks the calls and results for a Grift::MockMethod.

Defined Under Namespace

Classes: MockArguments

Instance Method Summary collapse

Constructor Details

#initializeGrift::MockMethod::MockExecutions

A new instance of MockExecutions.



14
15
16
# File 'lib/grift/mock_method/mock_executions.rb', line 14

def initialize
  @executions = []
end

Instance Method Details

#callsArray<Grift::MockMethod::MockExecutions::MockArguments>

Returns an array of the args used in each call to the mocked method

Examples:

my_mock = Grift.spy_on(Number, :+)
x = (3 + 4) + 5
my_mock.mock.calls.map(&:values)
#=> [[4], [5]]

Returns:



29
30
31
32
33
# File 'lib/grift/mock_method/mock_executions.rb', line 29

def calls
  @executions.map do |exec|
    exec[:arguments]
  end
end

#countNumber

Returns the count of executions.

Examples:

my_mock = Grift.mock(String, :upcase)
my_mock.mock.count
#=> 0
"apple".upcase
#=> "APPLE"
my_mock.mock.count
#=> 1

Returns:

  • (Number)

    the number of executions



67
68
69
# File 'lib/grift/mock_method/mock_executions.rb', line 67

def count
  @executions.count
end

#empty?Boolean

Returns true if there have been no calls tracked.

Examples:

my_mock = Grift.mock(String, :upcase)
my_mock.mock.empty?
#=> true
"apple".upcase
#=> "APPLE"
my_mock.mock.empty?
#=> false

Returns:

  • (Boolean)

    if the executions are empty



49
50
51
# File 'lib/grift/mock_method/mock_executions.rb', line 49

def empty?
  @executions.empty?
end

#resultsArray

Returns an array of the results of each call to the mocked method

Examples:

my_mock = Grift.spy_on(Number, :+)
x = (3 + 4) + 5
my_mock.mock.results
#=> [7, 12]

Returns:

  • (Array)

    an array of results



82
83
84
85
86
# File 'lib/grift/mock_method/mock_executions.rb', line 82

def results
  @executions.map do |exec|
    exec[:result]
  end
end

#store(args: [], kwargs: {}, result: nil) ⇒ Array

Stores an args and result pair to the executions array.

Examples:

mock_executions = Grift::MockMethod::MockExecutions.new
mock_executions.store(args: [1, 1], kwargs: { test: true }, result: 2)

Parameters:

  • args (Array) (defaults to: [])

    the postitional args to store

  • kwargs (Hash) (defaults to: {})

    the keyword args to store

  • result (defaults to: nil)

    the method result to store

Returns:

  • (Array)

    an updated array of executions



101
102
103
# File 'lib/grift/mock_method/mock_executions.rb', line 101

def store(args: [], kwargs: {}, result: nil)
  @executions.push({ arguments: MockArguments.new(args: args, kwargs: kwargs), result: result })
end