diff --git a/library/unittest.mock-examples.po b/library/unittest.mock-examples.po index 97ae5cb421..c38b74abf7 100644 --- a/library/unittest.mock-examples.po +++ b/library/unittest.mock-examples.po @@ -8,7 +8,7 @@ msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2023-09-09 00:03+0000\n" -"PO-Revision-Date: 2024-01-26 19:03+0800\n" +"PO-Revision-Date: 2024-03-26 20:46+0800\n" "Last-Translator: Liang-Bo Wang \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" @@ -627,7 +627,7 @@ msgstr "" #: ../../library/unittest.mock-examples.rst:601 msgid "Partial mocking" -msgstr "" +msgstr "部分 mocking" #: ../../library/unittest.mock-examples.rst:603 msgid "" @@ -637,6 +637,10 @@ msgid "" "in C, and so I couldn't just monkey-patch out the static :meth:`datetime." "date.today` method." msgstr "" +"在某些測試中,我們會想 mock 對 :meth:`datetime.date.today` 的呼叫以回傳一個已" +"知日期,但我不想阻止測試中的程式碼建立新的日期物件。不幸的是 :class:" +"`datetime.date` 是用 C 語言寫的,所以們我不能 monkey patch 靜態的 :meth:" +"`datetime.date.today` 方法。" #: ../../library/unittest.mock-examples.rst:608 msgid "" @@ -644,6 +648,8 @@ msgid "" "date class with a mock, but passing through calls to the constructor to the " "real class (and returning real instances)." msgstr "" +"我們找到了一種簡單的方法來做到這一點,其用 mock 有效地包裝日期類別,但將對建" +"構函式的呼叫傳遞給真實的類別(並返回真實的實例)。" #: ../../library/unittest.mock-examples.rst:612 msgid "" @@ -653,12 +659,18 @@ msgid "" "date. When the mock date class is called a real date will be constructed and " "returned by ``side_effect``. ::" msgstr "" +"這裡使用 :func:`patch 裝飾器 ` 來 mock 被測模組中的 ``date`` 類別。然" +"後,mock 日期類別上的 :attr:`~Mock.side_effect` 屬性會被設定為回傳真實日期的 " +"lambda 函式。當 mock 日期類別被呼叫時,將透過 ``side_effect`` 建構並回傳真實" +"日期。: ::" #: ../../library/unittest.mock-examples.rst:626 msgid "" "Note that we don't patch :class:`datetime.date` globally, we patch ``date`` " "in the module that *uses* it. See :ref:`where to patch `." msgstr "" +"注意,我們沒有全域 patch :class:`datetime.date`,而是在\\ *使用*\\ 它的模組" +"中 patch ``date``。請參閱 :ref:`該 patch 何處 `。" #: ../../library/unittest.mock-examples.rst:629 msgid "" @@ -667,12 +679,18 @@ msgid "" "find yourself having to calculate an expected result using exactly the same " "algorithm as the code under test, which is a classic testing anti-pattern." msgstr "" +"當 ``date.today()`` 被呼叫時,一個已知日期會被回傳,但對 ``date(...)`` 建構函" +"式的呼叫仍然會回傳正常日期。如果不這樣使用,你可能會發現自己必須使用與被測程" +"式碼完全相同的演算法來計算預期結果,這是一個典型的測試的反面模式 (anti-" +"pattern)。" #: ../../library/unittest.mock-examples.rst:634 msgid "" "Calls to the date constructor are recorded in the ``mock_date`` attributes " "(``call_count`` and friends) which may also be useful for your tests." msgstr "" +"對日期建構函式的呼叫被記錄在 ``mock_date`` 屬性(\\ ``call_count`` 及其相關屬" +"性)中,這對你的測試也可能有用處。" #: ../../library/unittest.mock-examples.rst:637 msgid "" @@ -680,16 +698,21 @@ msgid "" "is discussed in `this blog entry `_." msgstr "" +"處理 mock 日期或其他內建類別的另一種方法在 `這個 blog `_ 中" +"討論。" #: ../../library/unittest.mock-examples.rst:643 msgid "Mocking a Generator Method" -msgstr "" +msgstr "Mock 產生器方法" #: ../../library/unittest.mock-examples.rst:645 msgid "" "A Python generator is a function or method that uses the :keyword:`yield` " "statement to return a series of values when iterated over [#]_." msgstr "" +"Python 產生器是一個函式或方法,它使用 :keyword:`yield` 陳述式在疊代 [#]_ 時回" +"傳一系列的值。" #: ../../library/unittest.mock-examples.rst:648 msgid "" @@ -698,15 +721,18 @@ msgid "" "iteration is :meth:`~container.__iter__`, so we can mock this using a :class:" "`MagicMock`." msgstr "" +"產生器方法 / 函式會被呼叫以回傳產生器物件。之後此產生器會進行疊代。疊代的協定" +"方法是 :meth:`~container.__iter__`,所以我們可以使用 :class:`MagicMock` 來 " +"mock 它。" #: ../../library/unittest.mock-examples.rst:653 msgid "" "Here's an example class with an \"iter\" method implemented as a generator:" -msgstr "" +msgstr "下面是一個範例類別,其包含實作產生器的一個 \"iter\" 方法:" #: ../../library/unittest.mock-examples.rst:665 msgid "How would we mock this class, and in particular its \"iter\" method?" -msgstr "" +msgstr "我們該如何 mock 這個類別,特別是它的 \"iter\" 方法呢?" #: ../../library/unittest.mock-examples.rst:667 msgid "" @@ -714,6 +740,8 @@ msgid "" "to :class:`list`), we need to configure the object returned by the call to " "``foo.iter()``." msgstr "" +"要配置從疊代回傳的值(隱含在對 :class:`list` 的呼叫中),我們需要配置呼叫 " +"``foo.iter()`` 所回傳的物件。" #: ../../library/unittest.mock-examples.rst:675 msgid "" @@ -723,10 +751,14 @@ msgid "" "they are is: `Generator Tricks for Systems Programmers `_." msgstr "" +"還有關於產生器運算式及產生器的更多 `進階用法 `_ ,但我們在這裡不考慮它們。一個關於產生器及其強大功能" +"的優良說明是:`Generator Tricks for Systems Programmers `_。" #: ../../library/unittest.mock-examples.rst:683 msgid "Applying the same patch to every test method" -msgstr "" +msgstr "對每個測試方法應用相同的 patch" #: ../../library/unittest.mock-examples.rst:685 msgid "" @@ -737,6 +769,10 @@ msgid "" "methods on the class. A test method is identified by methods whose names " "start with ``test``::" msgstr "" +"如果你希望 patch 能用在多個測試方法上,顯而易見的方式是將 patch 裝飾器應用於" +"每個方法。這感覺是不必要的重複行為,因此你可以使用 :func:`patch`\\ (及其他 " +"patch 的變體)作為類別裝飾器。這會將 patch 應用在該類別的所有測試方法上。測試" +"方法由名稱以 ``test`` 開頭來識別: ::" #: ../../library/unittest.mock-examples.rst:709 msgid "" @@ -744,6 +780,8 @@ msgid "" "These allow you to move the patching into your ``setUp`` and ``tearDown`` " "methods. ::" msgstr "" +"管理 patch 的另一種方式是使用 :ref:`start-and-stop`。這允許你將 patch 移到你" +"的 ``setUp`` 與 ``tearDown`` 方法中。: ::" #: ../../library/unittest.mock-examples.rst:726 msgid "" @@ -752,10 +790,13 @@ msgid "" "exception is raised in the setUp then tearDown is not called. :meth:" "`unittest.TestCase.addCleanup` makes this easier::" msgstr "" +"如果你使用這個技巧,你必須確保透過呼叫 ``stop`` 來 \"取消\" patch。這可能會比" +"你想像的還要複雜一點,因為如果有例外在 ``setUp`` 中被引發,則 ``tearDown`` 就" +"不會被呼叫。:meth:`unittest.TestCase.addCleanup` 會讓這稍微簡單一點: ::" #: ../../library/unittest.mock-examples.rst:744 msgid "Mocking Unbound Methods" -msgstr "" +msgstr "Mock Unbound Methods (未繫結方法)" #: ../../library/unittest.mock-examples.rst:746 msgid "" @@ -770,6 +811,13 @@ msgid "" "to patch out methods with a mock that having to create a real function " "becomes a nuisance." msgstr "" +"在撰寫測試時,當我們需要 patch 一個\\ *未繫結方法*\\(patch 類別上的方法而不" +"是實例上的方法)。我們需要將 self 作為第一個引數傳入,因為我們想斷言哪些物件" +"正在呼叫這個特定方法。問題是你無法為此使用 mock 進行 patch,因為就算你用一個 " +"mock 替換未繫結方法,從實例取得它時它也不會成為一個繫結方法,因此 self 並不會" +"被傳遞。解決方法是使用真實的函式來 patch 未繫結方法。:func:`patch` 裝飾器使得" +"用 mock 來 patch out 方法是如此的簡單,以至於建立一個真正的函式相對變得很麻" +"煩。" #: ../../library/unittest.mock-examples.rst:757 msgid "" @@ -782,22 +830,29 @@ msgid "" "instance. It will have ``self`` passed in as the first argument, which is " "exactly what I wanted:" msgstr "" +"如果你將 ``autospec=True`` 傳遞給 patch,那麼它會使用\\ *真的*\\ 函式物件來進" +"行 patch。此函式物件與它所替換的函式物件具有相同的簽名,但實際上委託給 mock。" +"你仍然會以與之前完全相同的方式自動建立 mock。但這意味著,如果你使用它來 " +"patch 類別上的未繫結方法,則從實例取得的 mock 函式將轉換為繫結方法。``self`` " +"會作為其第一個引數傳入,而這正是我們想要的:" #: ../../library/unittest.mock-examples.rst:778 msgid "" "If we don't use ``autospec=True`` then the unbound method is patched out " "with a Mock instance instead, and isn't called with ``self``." msgstr "" +"如果我們不使用 ``autospec=True``,那麼未繫結方法將使用一個 Mock 實例 patch " +"out,並且不被使用 ``self`` 進行呼叫。" #: ../../library/unittest.mock-examples.rst:783 msgid "Checking multiple calls with mock" -msgstr "" +msgstr "使用 mock 檢查多個呼叫" #: ../../library/unittest.mock-examples.rst:785 msgid "" "mock has a nice API for making assertions about how your mock objects are " "used." -msgstr "" +msgstr "mock 有很好的 API,用於對 mock 物件的使用方式做出斷言。" #: ../../library/unittest.mock-examples.rst:792 msgid "" @@ -805,6 +860,8 @@ msgid "" "assert_called_once_with` method that also asserts that the :attr:`~Mock." "call_count` is one." msgstr "" +"如果你的 mock 只被呼叫一次,你可以使用 :meth:`~Mock.assert_called_once_with` " +"方法,其也斷言 :attr:`~Mock.call_count` 是1。" #: ../../library/unittest.mock-examples.rst:803 msgid "" @@ -813,6 +870,9 @@ msgid "" "times, and you want to make assertions about *all* those calls you can use :" "attr:`~Mock.call_args_list`:" msgstr "" +"``assert_called_with`` 和 ``assert_called_once_with`` 都對\\ *最近一次*\\ 的" +"呼叫做出斷言。如果你的 mock 將被多次呼叫,並且你想要對\\ *所有*\\ 這些呼叫進" +"行斷言,你可以使用 :attr:`~Mock.call_args_list`:" #: ../../library/unittest.mock-examples.rst:815 msgid "" @@ -821,10 +881,13 @@ msgid "" "``call_args_list``. This looks remarkably similar to the repr of the " "``call_args_list``:" msgstr "" +":data:`call` 輔助函式可以輕鬆地對這些呼叫做出斷言。你可以建立預期呼叫的清單並" +"將其與 ``call_args_list`` 進行比較。這看起來與 ``call_args_list`` 的 repr 非" +"常相似:" #: ../../library/unittest.mock-examples.rst:825 msgid "Coping with mutable arguments" -msgstr "" +msgstr "應對可變引數" #: ../../library/unittest.mock-examples.rst:827 msgid "" @@ -834,18 +897,22 @@ msgid "" "under test then you can no longer make assertions about what the values were " "when the mock was called." msgstr "" +"另一種情況很少見,但可能會困擾你,那就是當你的 mock 被使用可變引數呼叫。" +"``call_args`` 和 ``call_args_list`` 儲存對引數的\\ *參照*。如果引數被測試中的" +"程式碼改變,那麼你將無法再對 mock 被呼叫時的值進行斷言。" #: ../../library/unittest.mock-examples.rst:832 msgid "" "Here's some example code that shows the problem. Imagine the following " "functions defined in 'mymodule'::" -msgstr "" +msgstr "這是一些秀出問題的程式碼範例。 想像 'mymodule' 中定義以下函式: ::" #: ../../library/unittest.mock-examples.rst:843 msgid "" "When we try to test that ``grob`` calls ``frob`` with the correct argument " "look what happens::" msgstr "" +"當我們嘗試測試 ``grob`` 使用正確的引數呼叫 ``frob`` 時,看看會發生什麼: ::" #: ../../library/unittest.mock-examples.rst:858 msgid "" @@ -853,6 +920,8 @@ msgid "" "could then cause problems if you do assertions that rely on object identity " "for equality." msgstr "" +"一種可能是讓 mock 複製你傳入的引數。如果你進行的斷言依賴於物件識別性來確定相" +"等性,這就可能導致問題。" #: ../../library/unittest.mock-examples.rst:862 msgid "" @@ -864,6 +933,11 @@ msgid "" "methods for doing the assertion. Again a helper function sets this up for " "me. ::" msgstr "" +"以下是一種使用 :attr:`~Mock.side_effect` 功能的解法。如果你為 mock 提供一個 " +"``side_effect`` 函式,則 ``side_effect`` 將被使用與 mock 相同的引數呼叫。這使" +"我們有機會複製引數並將其儲存以供之後的斷言。在這個範例中,我們使用\\ *另一個" +"* mock 來儲存引數,以便我們可以使用 mock 方法來執行斷言。同樣的,有一個輔助函" +"式為我們設定了這些。: ::" #: ../../library/unittest.mock-examples.rst:891 msgid "" @@ -871,6 +945,9 @@ msgid "" "new mock that we do the assertion on. The ``side_effect`` function makes a " "copy of the args and calls our ``new_mock`` with the copy." msgstr "" +"``copy_call_args`` 與將要被呼叫的 mock 一起被呼叫。它回傳一個我們會對其進行斷" +"言的新的 mock。``side_effect`` 函式建立引數們的副本,並用該副本呼叫我們的 " +"``new_mock``。" #: ../../library/unittest.mock-examples.rst:897 msgid "" @@ -878,6 +955,8 @@ msgid "" "checking arguments at the point they are called. You can simply do the " "checking inside a ``side_effect`` function." msgstr "" +"如果你的 mock 只會被使用一次,則有一種更簡單的方法可以在呼叫引數時檢查它們。" +"你可以簡單地在 ``side_effect`` 函式內進行檢查。" #: ../../library/unittest.mock-examples.rst:911 msgid "" @@ -885,6 +964,8 @@ msgid "" "`MagicMock` that copies (using :func:`copy.deepcopy`) the arguments. Here's " "an example implementation:" msgstr "" +"另一種方法是建立 :class:`Mock` 或 :class:`MagicMock` 的子類別來複製(使用 :" +"func:`copy.deepcopy`\\ )引數。這是一個實作的例子:" #: ../../library/unittest.mock-examples.rst:935 msgid "" @@ -893,10 +974,13 @@ msgid "" "That means all children of a ``CopyingMock`` will also have the type " "``CopyingMock``." msgstr "" +"當你將 ``Mock`` 或 ``MagicMock`` 子類別化時,所有屬性會被動態建立,且 " +"``return_value`` 會自動使用你的子類別。這代表著 ``CopyingMock`` 的所有子代 " +"(child) 也會具有``CopyingMock`` 型別。" #: ../../library/unittest.mock-examples.rst:941 msgid "Nesting Patches" -msgstr "" +msgstr "巢狀使用 Patch" #: ../../library/unittest.mock-examples.rst:943 msgid "" @@ -904,6 +988,8 @@ msgid "" "can end up with nested with statements indenting further and further to the " "right::" msgstr "" +"將 patch 作為情境管理器使用很好,但是如果你使用複數個 patch,你最終可能會得到" +"巢狀的 with 陳述式,並且越來越向右縮排: ::" #: ../../library/unittest.mock-examples.rst:961 msgid "" @@ -912,16 +998,21 @@ msgid "" "method, ``create_patch``, puts the patch in place and returns the created " "mock for us::" msgstr "" +"我們可以使用 unittest 的 ``cleanup`` 函式以及 :ref:`start-and-stop` 來達到相" +"同的效果,而不會出現因巢狀導致的縮排。一個簡單的輔助方法 ``create_patch`` 會" +"將 patch 放置到位並為我們回傳被建立的 mock: ::" #: ../../library/unittest.mock-examples.rst:989 msgid "Mocking a dictionary with MagicMock" -msgstr "" +msgstr "使用 MagicMock 來 mock 字典" #: ../../library/unittest.mock-examples.rst:991 msgid "" "You may want to mock a dictionary, or other container object, recording all " "access to it whilst having it still behave like a dictionary." msgstr "" +"你可能會想要 mock 字典或其他容器物件,記錄對它的所有存取,同時讓它仍然像字典" +"一樣運作。" #: ../../library/unittest.mock-examples.rst:994 msgid "" @@ -929,6 +1020,8 @@ msgid "" "and using :data:`~Mock.side_effect` to delegate dictionary access to a real " "underlying dictionary that is under our control." msgstr "" +"我們可以使用 :class:`MagicMock` 來做到這一點,它的行為會與字典一致,並使用 :" +"data:`~Mock.side_effect` 將字典存取委託給我們控制下的真實底層字典。" #: ../../library/unittest.mock-examples.rst:998 msgid "" @@ -937,18 +1030,26 @@ msgid "" "``side_effect`` is called with the key (and in the case of ``__setitem__`` " "the value too). We can also control what is returned." msgstr "" +"當 ``MagicMock`` 的 :meth:`~object.__getitem__` 和 :meth:`~object." +"__setitem__` 方法被呼叫時(一般的字典存取), ``side_effect`` 會被使用鍵 " +"(key) 來呼叫 (在 ``__setitem__`` 的情況也會使用值 (value))。我們也可以控制" +"回傳的內容。" #: ../../library/unittest.mock-examples.rst:1003 msgid "" "After the ``MagicMock`` has been used we can use attributes like :data:" "`~Mock.call_args_list` to assert about how the dictionary was used:" msgstr "" +"使用 ``MagicMock`` 後,我們可以使用諸如 :data:`~Mock.call_args_list` 之類的屬" +"性來斷言字典被使用的方式:" #: ../../library/unittest.mock-examples.rst:1019 msgid "" "An alternative to using ``MagicMock`` is to use ``Mock`` and *only* provide " "the magic methods you specifically want:" msgstr "" +"不使用 ``MagicMock`` 的替代方案是使用 ``Mock`` 並且\\ *只*\\ 提供你特別想要的" +"魔術方法:" #: ../../library/unittest.mock-examples.rst:1026 msgid "" @@ -956,6 +1057,8 @@ msgid "" "*spec* (or *spec_set*) argument so that the ``MagicMock`` created only has " "dictionary magic methods available:" msgstr "" +"*第三個*\\ 選擇是使用 ``MagicMock``,但傳入 ``dict`` 作為 *spec*(或" +"*spec_set*)引數,以使被建立的 ``MagicMock`` 僅具有字典可用的魔術方法:" #: ../../library/unittest.mock-examples.rst:1034 msgid "" @@ -963,16 +1066,18 @@ msgid "" "normal dictionary but recording the access. It even raises a :exc:`KeyError` " "if you try to access a key that doesn't exist." msgstr "" +"有了這些 side effect 函式,``mock`` 會像一般的字典一樣運作,但會記錄存取。如" +"果你嘗試存取不存在的鍵,它甚至會引發一個 :exc:`KeyError`。" #: ../../library/unittest.mock-examples.rst:1053 msgid "" "After it has been used you can make assertions about the access using the " "normal mock methods and attributes:" -msgstr "" +msgstr "在上述方式被使用後,你就可以使用普通的 mock 方法和屬性對存取進行斷言:" #: ../../library/unittest.mock-examples.rst:1065 msgid "Mock subclasses and their attributes" -msgstr "" +msgstr "Mock 子類別及其屬性" #: ../../library/unittest.mock-examples.rst:1067 msgid ""