Module: Taski::TestHelper

Defined in:
lib/taski/test_helper.rb,
lib/taski/test_helper/rspec.rb,
lib/taski/test_helper/errors.rb,
lib/taski/test_helper/minitest.rb,
lib/taski/test_helper/mock_wrapper.rb,
lib/taski/test_helper/mock_registry.rb

Overview

Test helper module for mocking Taski task dependencies in unit tests. Include this module in your test class to access mocking functionality.

Examples:

Minitest usage

class BuildReportTest < Minitest::Test
  include Taski::TestHelper

  def test_builds_report
    mock_task(FetchData, users: [1, 2, 3])
    assert_equal 3, BuildReport.user_count
  end
end

Defined Under Namespace

Modules: Minitest, MockRegistry, RSpec, SchedulerExtension, TaskExtension, WorkerPoolExtension Classes: InvalidMethodError, InvalidTaskError, MockWrapper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mock_for(task_class) ⇒ MockWrapper?

Retrieves the mock wrapper for a task class.

Parameters:

  • task_class (Class)

    The task class to look up

Returns:

  • (MockWrapper, nil)

    The mock wrapper or nil if not mocked



90
91
92
# File 'lib/taski/test_helper.rb', line 90

def mock_for(task_class)
  MockRegistry.mock_for(task_class)
end

.mocks_active?Boolean

Checks if any mocks are currently registered.

Returns:

  • (Boolean)

    true if mocks exist



83
84
85
# File 'lib/taski/test_helper.rb', line 83

def mocks_active?
  MockRegistry.mocks_active?
end

.reset_mocks!Object

Clears all registered mocks. Called automatically by test framework integrations.



96
97
98
# File 'lib/taski/test_helper.rb', line 96

def reset_mocks!
  MockRegistry.reset!
end

Instance Method Details

#assert_task_accessed(task_class, method_name) ⇒ true

Asserts that a mocked task's method was accessed during the test.

Parameters:

  • task_class (Class)

    The mocked task class

  • method_name (Symbol)

    The exported method name

Returns:

  • (true)

    If assertion passes

Raises:

  • (ArgumentError)

    If task_class was not mocked

  • (Minitest::Assertion, RSpec::Expectations::ExpectationNotMetError)

    If method was not accessed



142
143
144
145
146
147
148
149
150
# File 'lib/taski/test_helper.rb', line 142

def assert_task_accessed(task_class, method_name)
  mock = fetch_mock!(task_class)

  unless mock.accessed?(method_name)
    raise assertion_error("Expected #{task_class}.#{method_name} to be accessed, but it was not")
  end

  true
end

#mock_env(root_task: nil) ⇒ Taski::Env

Sets mock env for the duration of the test. This allows testing code that depends on Taski.env without running full task execution. Env is automatically cleared when MockRegistry.reset! is called (in test teardown).

Examples:

mock_env(root_task: MyTask)
assert_equal MyTask, Taski.env.root_task

Parameters:

  • root_task (Class) (defaults to: nil)

    The root task class (defaults to nil for testing)

Returns:



110
111
112
113
114
# File 'lib/taski/test_helper.rb', line 110

def mock_env(root_task: nil)
  Taski.reset_env!
  Taski.send(:start_env, root_task: root_task)
  Taski.env
end

#mock_task(task_class, **values) ⇒ MockWrapper

Registers a mock for a task class with specified return values.

Examples:

mock_task(FetchData, result: { users: [1, 2, 3] })
mock_task(Config, timeout: 30, retries: 3)

Parameters:

  • task_class (Class)

    A Taski::Task subclass

  • values (Hash{Symbol => Object})

    Method names mapped to return values

Returns:

Raises:



126
127
128
129
130
131
132
133
# File 'lib/taski/test_helper.rb', line 126

def mock_task(task_class, **values)
  validate_task_class!(task_class)
  validate_exported_methods!(task_class, values.keys)

  mock_wrapper = MockWrapper.new(task_class, values)
  MockRegistry.register(task_class, mock_wrapper)
  mock_wrapper
end

#refute_task_accessed(task_class, method_name) ⇒ true

Asserts that a mocked task's method was NOT accessed during the test.

Parameters:

  • task_class (Class)

    The mocked task class

  • method_name (Symbol)

    The exported method name

Returns:

  • (true)

    If assertion passes

Raises:

  • (ArgumentError)

    If task_class was not mocked

  • (Minitest::Assertion, RSpec::Expectations::ExpectationNotMetError)

    If method was accessed



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/taski/test_helper.rb', line 159

def refute_task_accessed(task_class, method_name)
  mock = fetch_mock!(task_class)

  if mock.accessed?(method_name)
    count = mock.access_count(method_name)
    raise assertion_error(
      "Expected #{task_class}.#{method_name} not to be accessed, but it was accessed #{count} time(s)"
    )
  end

  true
end