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.
Defined Under Namespace
Modules: Minitest, MockRegistry, RSpec, SchedulerExtension, TaskExtension, WorkerPoolExtension Classes: InvalidMethodError, InvalidTaskError, MockWrapper
Class Method Summary collapse
-
.mock_for(task_class) ⇒ MockWrapper?
Retrieves the mock wrapper for a task class.
-
.mocks_active? ⇒ Boolean
Checks if any mocks are currently registered.
-
.reset_mocks! ⇒ Object
Clears all registered mocks.
Instance Method Summary collapse
-
#assert_task_accessed(task_class, method_name) ⇒ true
Asserts that a mocked task's method was accessed during the test.
-
#mock_env(root_task: nil) ⇒ Taski::Env
Sets mock env for the duration of the test.
-
#mock_task(task_class, **values) ⇒ MockWrapper
Registers a mock for a task class with specified return values.
-
#refute_task_accessed(task_class, method_name) ⇒ true
Asserts that a mocked task's method was NOT accessed during the test.
Class Method Details
.mock_for(task_class) ⇒ MockWrapper?
Retrieves the mock wrapper for a task class.
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.
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.
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).
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.
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.
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 |