Skip to content

Commit 2c1b249

Browse files
committed
Use mocks instead of instances
1 parent 6b014b5 commit 2c1b249

2 files changed

Lines changed: 34 additions & 26 deletions

File tree

tests/back_end/test_platform_handler.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,39 @@
3737
class TestPlatformHandler:
3838
"""Tests for the PlatformHandler class."""
3939

40+
@pytest.fixture
41+
def console(self, mocker: MockerFixture) -> Console:
42+
"""Console instance for testing."""
43+
44+
def exception_to_string(exception: Exception) -> str:
45+
return str(exception)
46+
47+
# Create mock.
48+
mocked_console = mocker.Mock(spec=Console)
49+
50+
# Patch internally used functions.
51+
_ = mocker.patch.object(mocked_console, "pretty_exception", side_effect=exception_to_string)
52+
53+
# Return mocked console.
54+
return mocked_console
55+
56+
@pytest.fixture
57+
def binding(self, mocker: MockerFixture) -> BaseBinding:
58+
"""FakeBinding instance for testing."""
59+
60+
def reflect_vector4(input_vector: Vector4) -> Vector4:
61+
return input_vector
62+
63+
# Create mock.
64+
mocked_binding = mocker.Mock(spec=BaseBinding)
65+
66+
# Patch internally used functions.
67+
_ = mocker.patch.object(mocked_binding, "platform_space_to_unified_space", side_effect=reflect_vector4)
68+
_ = mocker.patch.object(mocked_binding, "unified_space_to_platform_space", side_effect=reflect_vector4)
69+
70+
# Return mocked binding.
71+
return mocked_binding
72+
4073
@pytest.fixture
4174
def platform_handler(self, binding: BaseBinding, console: Console) -> PlatformHandler:
4275
"""Fixture for creating a PlatformHandler instance."""
@@ -546,7 +579,7 @@ async def test_emergency_stop(
546579
"""Platform should call stop_all and print to critical console."""
547580
# Mock binding.
548581
patched_stop_all = mocker.patch.object(
549-
PlatformHandler,
582+
platform_handler,
550583
"stop_all",
551584
)
552585
spied_critical_print = mocker.spy(console, "critical_print")

tests/conftest.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import pytest
2-
from pytest_mock import MockerFixture
31
from vbl_aquarium.models.unity import Vector3, Vector4
42

5-
from ephys_link.front_end.console import Console
6-
from ephys_link.utils.base_binding import BaseBinding
7-
83
# Dummy values for testing.
94
DUMMY_STRING = "Dummy String"
105
DUMMY_SMALL_STRING = "dummy"
@@ -13,23 +8,3 @@
138
DUMMY_VECTOR3 = Vector3(x=1.0, y=2.0, z=3.0)
149
DUMMY_VECTOR4 = Vector4(x=1.0, y=2.0, z=3.0, w=4.0)
1510
DUMMY_EXCEPTION = RuntimeError("Test runtime error")
16-
17-
18-
@pytest.fixture
19-
def console() -> Console:
20-
"""Console instance for testing."""
21-
return Console()
22-
23-
24-
@pytest.fixture
25-
def binding(mocker: MockerFixture) -> BaseBinding:
26-
"""FakeBinding instance for testing."""
27-
28-
def reflect_vector4(input_vector: Vector4) -> Vector4:
29-
return input_vector
30-
31-
# Patch the BaseBinding class.
32-
mocked_binding = mocker.Mock(spec=BaseBinding)
33-
_ = mocker.patch.object(mocked_binding, "platform_space_to_unified_space", side_effect=reflect_vector4)
34-
_ = mocker.patch.object(mocked_binding, "unified_space_to_platform_space", side_effect=reflect_vector4)
35-
return mocked_binding

0 commit comments

Comments
 (0)