|
14 | 14 |
|
15 | 15 | @pytest.fixture |
16 | 16 | def mock_file_svc(): |
17 | | - from src.services.file_service import FileService |
18 | | - |
19 | 17 | svc = MagicMock() |
20 | 18 | svc.open_file.return_value = TextDocument( |
21 | 19 | filepath="/tmp/test.txt", content="hello world", encoding="utf-8" |
22 | 20 | ) |
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 |
28 | 21 | return svc |
29 | 22 |
|
30 | 23 |
|
@@ -462,19 +455,33 @@ def test_char_count_updates_on_delete_without_cursor_move(self, window, qtbot): |
462 | 455 | assert "8 chars" in window._cursor_label.text() |
463 | 456 |
|
464 | 457 |
|
| 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 | + |
465 | 472 | 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): |
467 | 474 | """getSaveFileName returns path → file saved to that path.""" |
468 | 475 | out = tmp_path / "renamed.txt" |
469 | 476 | monkeypatch.setattr( |
470 | 477 | "src.views.main_window.QFileDialog.getSaveFileName", |
471 | 478 | lambda *a, **kw: (str(out), ""), |
472 | 479 | ) |
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() |
476 | 483 | 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) |
478 | 485 |
|
479 | 486 | def test_cancelled_dialog_is_no_op(self, window, monkeypatch): |
480 | 487 | """getSaveFileName returns '' → nothing saved.""" |
|
0 commit comments