Skip to content

Commit 47566b0

Browse files
chrisdpurcellclaude
andcommitted
refactor: scope save_file side_effect to dedicated fixture, not shared mock
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent dd178b8 commit 47566b0

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

tests/integration/test_main_window.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,10 @@
1414

1515
@pytest.fixture
1616
def mock_file_svc():
17-
from src.services.file_service import FileService
18-
1917
svc = MagicMock()
2018
svc.open_file.return_value = TextDocument(
2119
filepath="/tmp/test.txt", content="hello world", encoding="utf-8"
2220
)
23-
# Delegate save_file to the real FileService so tests that write files to
24-
# tmp_path can verify disk contents. Tests that only check signals are
25-
# unaffected because they don't inspect file system state.
26-
_real_svc = FileService()
27-
svc.save_file.side_effect = _real_svc.save_file
2821
return svc
2922

3023

@@ -462,19 +455,33 @@ def test_char_count_updates_on_delete_without_cursor_move(self, window, qtbot):
462455
assert "8 chars" in window._cursor_label.text()
463456

464457

458+
@pytest.fixture
459+
def _real_save_svc(mock_file_svc):
460+
"""Extends mock_file_svc with real FileService.save_file for disk-write assertions."""
461+
from src.services.file_service import FileService
462+
mock_file_svc.save_file.side_effect = FileService().save_file
463+
return mock_file_svc
464+
465+
466+
@pytest.fixture
467+
def window_real_save(_real_save_svc, mock_text_svc, qapp):
468+
vm = MainViewModel(_real_save_svc, mock_text_svc)
469+
return MainWindow(vm)
470+
471+
465472
class TestActionSaveAsHandler:
466-
def test_chosen_path_is_saved(self, window, tmp_path, monkeypatch, qtbot):
473+
def test_chosen_path_is_saved(self, window_real_save, tmp_path, monkeypatch, qtbot):
467474
"""getSaveFileName returns path → file saved to that path."""
468475
out = tmp_path / "renamed.txt"
469476
monkeypatch.setattr(
470477
"src.views.main_window.QFileDialog.getSaveFileName",
471478
lambda *a, **kw: (str(out), ""),
472479
)
473-
window._plain_text_edit.setPlainText("save-as content")
474-
with qtbot.waitSignal(window._viewmodel.file_saved, timeout=2000):
475-
window._on_action_save_as()
480+
window_real_save._plain_text_edit.setPlainText("save-as content")
481+
with qtbot.waitSignal(window_real_save._viewmodel.file_saved, timeout=2000):
482+
window_real_save._on_action_save_as()
476483
assert out.read_text(encoding="utf-8") == "save-as content"
477-
assert window._file_name_edit.text() == str(out)
484+
assert window_real_save._file_name_edit.text() == str(out)
478485

479486
def test_cancelled_dialog_is_no_op(self, window, monkeypatch):
480487
"""getSaveFileName returns '' → nothing saved."""

0 commit comments

Comments
 (0)