Class: Taski::TestHelper::MockWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/taski/test_helper/mock_wrapper.rb

Overview

Wraps a mocked task and returns pre-configured values without executing the task. Tracks which methods were accessed for verification in tests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_class, mock_values) ⇒ MockWrapper

Returns a new instance of MockWrapper.

Parameters:

  • task_class (Class)

    The task class being mocked

  • mock_values (Hash{Symbol => Object})

    Method names mapped to their return values



12
13
14
15
16
# File 'lib/taski/test_helper/mock_wrapper.rb', line 12

def initialize(task_class, mock_values)
  @task_class = task_class
  @mock_values = mock_values
  @access_counts = Hash.new(0)
end

Instance Attribute Details

#mock_valuesObject (readonly)

Returns the value of attribute mock_values.



8
9
10
# File 'lib/taski/test_helper/mock_wrapper.rb', line 8

def mock_values
  @mock_values
end

#task_classObject (readonly)

Returns the value of attribute task_class.



8
9
10
# File 'lib/taski/test_helper/mock_wrapper.rb', line 8

def task_class
  @task_class
end

Instance Method Details

#access_count(method_name) ⇒ Integer

Returns the number of times a method was accessed.

Parameters:

  • method_name (Symbol)

    The method name to check

Returns:

  • (Integer)

    Number of accesses (0 if never accessed)



41
42
43
# File 'lib/taski/test_helper/mock_wrapper.rb', line 41

def access_count(method_name)
  @access_counts[method_name]
end

#accessed?(method_name) ⇒ Boolean

Checks if a method was accessed at least once.

Parameters:

  • method_name (Symbol)

    The method name to check

Returns:

  • (Boolean)

    true if accessed at least once



34
35
36
# File 'lib/taski/test_helper/mock_wrapper.rb', line 34

def accessed?(method_name)
  @access_counts[method_name] > 0
end

#get_exported_value(method_name) ⇒ Object

Returns the mocked value for a method and records the access.

Parameters:

  • method_name (Symbol)

    The exported method name

Returns:

  • (Object)

    The pre-configured mock value

Raises:

  • (KeyError)

    If method_name was not configured in the mock



22
23
24
25
26
27
28
29
# File 'lib/taski/test_helper/mock_wrapper.rb', line 22

def get_exported_value(method_name)
  unless @mock_values.key?(method_name)
    raise KeyError, "No mock value for method :#{method_name} on #{@task_class}"
  end

  @access_counts[method_name] += 1
  @mock_values[method_name]
end