@@ -8,7 +8,7 @@ msgstr ""
8
8
"Project-Id-Version : Python 3.12\n "
9
9
"Report-Msgid-Bugs-To : \n "
10
10
"POT-Creation-Date : 2023-09-09 00:03+0000\n "
11
- "PO-Revision-Date : 2024-01-14 23:14 +0800\n "
11
+ "PO-Revision-Date : 2024-01-17 01:09 +0800\n "
12
12
"
Last-Translator :
Liang-Bo Wang <[email protected] >\n "
13
13
"Language-Team : Chinese - TAIWAN (https://github.com/python/python-docs-zh- "
14
14
"tw)\n "
@@ -467,10 +467,13 @@ msgid ""
467
467
"argument (or :func:`patch.object` with two arguments). The mock will be "
468
468
"created for you and passed into the test function / method:"
469
469
msgstr ""
470
+ "如果你想使用一個 mock 進行 patch,你可以使用僅帶有一個引數的 :func:`patch`/ "
471
+ "(或帶有兩個引數的 :func:`patch.object`/ )。Mock 將被建立並被傳遞到測試函"
472
+ "式 / 方法中:"
470
473
471
474
#: ../../library/unittest.mock-examples.rst:471
472
475
msgid "You can stack up multiple patch decorators using this pattern::"
473
- msgstr ""
476
+ msgstr "你可以使用這個模式堆疊多個 patch 裝飾器: :: "
474
477
475
478
#: ../../library/unittest.mock-examples.rst:482
476
479
msgid ""
@@ -479,44 +482,54 @@ msgid ""
479
482
"decorators are applied). This means from the bottom up, so in the example "
480
483
"above the mock for ``test_module.ClassName2`` is passed in first."
481
484
msgstr ""
485
+ "當你嵌套 patch 裝飾器時,mock 會以被應用的順序傳遞到裝飾函數(裝飾器應用的正"
486
+ "常 *Python* 順序)。這意味著由下而上,因此在上面的範例中,\\ ``module."
487
+ "ClassName2`` 的 mock 會先被傳入。"
482
488
483
489
#: ../../library/unittest.mock-examples.rst:487
484
490
msgid ""
485
491
"There is also :func:`patch.dict` for setting values in a dictionary just "
486
492
"during a scope and restoring the dictionary to its original state when the "
487
493
"test ends:"
488
494
msgstr ""
495
+ "也有 :func:`patch.dict`,用於在測試範圍中設定字典內的值,並在測試結束時將其恢"
496
+ "復為原始狀態:"
489
497
490
498
#: ../../library/unittest.mock-examples.rst:498
491
499
msgid ""
492
500
"``patch``, ``patch.object`` and ``patch.dict`` can all be used as context "
493
501
"managers."
494
502
msgstr ""
503
+ "``patch``、``patch.object`` 和 ``patch.dict`` 都可以用來作為情境管理器。"
495
504
496
505
#: ../../library/unittest.mock-examples.rst:500
497
506
msgid ""
498
507
"Where you use :func:`patch` to create a mock for you, you can get a "
499
508
"reference to the mock using the \" as\" form of the with statement:"
500
509
msgstr ""
510
+ "當你使用 :func:`patch` 為你建立一個 mock 時,你可以使用 with 陳述式的 \" as\" "
511
+ "形式來取得 mock 的參照:"
501
512
502
513
#: ../../library/unittest.mock-examples.rst:515
503
514
msgid ""
504
515
"As an alternative ``patch``, ``patch.object`` and ``patch.dict`` can be used "
505
516
"as class decorators. When used in this way it is the same as applying the "
506
517
"decorator individually to every method whose name starts with \" test\" ."
507
518
msgstr ""
519
+ "另外,“patch”、“patch.object” 和 “patch.dict” 也可以用來作為類別裝飾器。以這"
520
+ "種方式使用時,與將裝飾器單獨應用於名稱以 “test” 開頭的每個方法相同。"
508
521
509
522
#: ../../library/unittest.mock-examples.rst:523
510
523
msgid "Further Examples"
511
- msgstr ""
524
+ msgstr "更多例子 "
512
525
513
526
#: ../../library/unittest.mock-examples.rst:526
514
527
msgid "Here are some more examples for some slightly more advanced scenarios."
515
- msgstr ""
528
+ msgstr "以下是一些更進階一點的情境的範例。 "
516
529
517
530
#: ../../library/unittest.mock-examples.rst:530
518
531
msgid "Mocking chained calls"
519
- msgstr ""
532
+ msgstr "Mock 鍊接呼叫 "
520
533
521
534
#: ../../library/unittest.mock-examples.rst:532
522
535
msgid ""
@@ -525,30 +538,40 @@ msgid ""
525
538
"for the first time, or you fetch its ``return_value`` before it has been "
526
539
"called, a new :class:`Mock` is created."
527
540
msgstr ""
541
+ "一旦你了解了 :attr:`~Mock.return_value` 屬性,mock 鏈接呼叫其實就很簡單了。當"
542
+ "一個 mock 第一次被呼叫,或者你在它被呼叫之前取得其 ``return_value`` 時,一個"
543
+ "新的 :class:`Mock` 就會被建立。"
528
544
529
545
#: ../../library/unittest.mock-examples.rst:537
530
546
msgid ""
531
547
"This means that you can see how the object returned from a call to a mocked "
532
548
"object has been used by interrogating the ``return_value`` mock:"
533
549
msgstr ""
550
+ "這代表你可以透過訊問 (interrogate) / ``return_value`` mock 來了解一個對被 "
551
+ "mock 的物件的呼叫回傳的物件是如何被使用的:"
534
552
535
553
#: ../../library/unittest.mock-examples.rst:545
536
554
msgid ""
537
555
"From here it is a simple step to configure and then make assertions about "
538
556
"chained calls. Of course another alternative is writing your code in a more "
539
557
"testable way in the first place..."
540
558
msgstr ""
559
+ "從這裡開始,只需一個簡單的步驟即可進行配置並對鍊接呼叫進行斷言。 當然,另一種"
560
+ "選擇是先以更容易被測試的方式撰寫程式碼..."
541
561
542
562
#: ../../library/unittest.mock-examples.rst:549
543
563
msgid "So, suppose we have some code that looks a little bit like this:"
544
- msgstr ""
564
+ msgstr "所以,假設我們有一些程式碼,看起來大概像這樣: "
545
565
546
566
#: ../../library/unittest.mock-examples.rst:558
547
567
msgid ""
548
568
"Assuming that ``BackendProvider`` is already well tested, how do we test "
549
569
"``method()``? Specifically, we want to test that the code section ``# more "
550
570
"code`` uses the response object in the correct way."
551
571
msgstr ""
572
+ "假設 ``BackendProvider`` 已經經過充分測試,那麼我們該如何測試 ``method()``? "
573
+ "具體來說,我們要測試程式碼部分 ``# more code`` 是否以正確的方式使用 "
574
+ "``response`` 物件。"
552
575
553
576
#: ../../library/unittest.mock-examples.rst:562
554
577
msgid ""
@@ -559,25 +582,37 @@ msgid ""
559
582
"assume the object it returns is 'file-like', so we'll ensure that our "
560
583
"response object uses the builtin :func:`open` as its ``spec``."
561
584
msgstr ""
585
+ "由於此呼叫鍊是從實例屬性進行的,因此我們可以在 ``Something`` 實例上 monkey "
586
+ "patch ``backend`` 屬性。在這種特定的情況下,我們只對最終呼叫 ``start_call`` "
587
+ "的回傳值感興趣,因此我們不需要做太多配置。我們假設它傳回的物件是類檔案物件 "
588
+ "(file-like),因此我們會確保我們的 response 物件使用內建的 :func:`open` 作為"
589
+ "其 ``spec``。"
562
590
563
591
#: ../../library/unittest.mock-examples.rst:569
564
592
msgid ""
565
593
"To do this we create a mock instance as our mock backend and create a mock "
566
594
"response object for it. To set the response as the return value for that "
567
595
"final ``start_call`` we could do this::"
568
596
msgstr ""
597
+ "為此,我們建立一個 mock 實例作為我們的 mock backend,並為其建立一個 mock "
598
+ "response 物件。要將 response 設定為最後的 ``start_call`` 的回傳值,我們可以這"
599
+ "樣做: ::"
569
600
570
601
#: ../../library/unittest.mock-examples.rst:575
571
602
msgid ""
572
603
"We can do that in a slightly nicer way using the :meth:`~Mock."
573
604
"configure_mock` method to directly set the return value for us::"
574
605
msgstr ""
606
+ "我們可以使用 :meth:`~Mock.configure_mock` 方法來以稍為友善一點的方式為我們直"
607
+ "接設定回傳值: ::"
575
608
576
609
#: ../../library/unittest.mock-examples.rst:584
577
610
msgid ""
578
611
"With these we monkey patch the \" mock backend\" in place and can make the "
579
612
"real call::"
580
613
msgstr ""
614
+ "有了這些,我們就可以將 \" mock backend\" monkey patch 到位,並且可以進行真正的"
615
+ "呼叫: ::"
581
616
582
617
#: ../../library/unittest.mock-examples.rst:590
583
618
msgid ""
@@ -586,6 +621,9 @@ msgid ""
586
621
"be several entries in ``mock_calls``. We can use :meth:`call.call_list` to "
587
622
"create this list of calls for us::"
588
623
msgstr ""
624
+ "藉由使用 :attr:`~Mock.mock_calls`,我們可以使用一個斷言來檢查鍊接呼叫。一個鍊"
625
+ "接呼叫是一行程式碼中的多個呼叫,因此 ``mock_calls`` 中會有多個條目。我們可以"
626
+ "使用 :meth:`call.call_list` 來為我們建立這個呼叫串列: ::"
589
627
590
628
#: ../../library/unittest.mock-examples.rst:601
591
629
msgid "Partial mocking"
0 commit comments