diff --git a/library/unittest.mock.po b/library/unittest.mock.po index 9d5c806de1..d1cbb0496a 100644 --- a/library/unittest.mock.po +++ b/library/unittest.mock.po @@ -7,7 +7,7 @@ msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2023-12-16 00:03+0000\n" -"PO-Revision-Date: 2024-02-19 21:27+0800\n" +"PO-Revision-Date: 2024-03-19 00:53+0800\n" "Last-Translator: Adrian Liaw \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" @@ -797,10 +797,8 @@ msgid "" "whether they were passed positionally or by name::" msgstr "" "在匹配對 mock 的呼叫時,使用 *spec*\\ (或 *spec_set*\\ )建立的可呼叫 mock " -"將會內省規格物件的簽名 (signature)。因此,它可以匹配實際呼叫的引數,無論它們" -"是按位置傳遞還是按名稱傳遞:\n" -"\n" -"::" +"將會內省 (introspect) 規格物件的簽名 (signature)。因此,它可以匹配實際呼叫的" +"引數,無論它們是按位置傳遞還是按名稱傳遞: :: #: ../../library/unittest.mock.rst:812 msgid "" @@ -2043,9 +2041,9 @@ msgid "" "``__rshift__``, ``__and__``, ``__xor__``, ``__or__``, and ``__pow__``" msgstr "" "數值方法(包括右側 (right hand) 和原地 (in-place) 變體):``__add__``、" -"``__sub__``、``__mul__``、``__matmul__``、``__truediv__``、" -"``__floordiv__``、 ``__mod__``、``__divmod__``、``__lshift__``、" -"``__rshift__``、``__and__``、``__xor__``、``__or__`` 和 ``__pow__``" +"``__sub__``、``__mul__``、``__matmul__``、``__truediv__``、``__floordiv__``、" +"``__mod__``、``__divmod__``、``__lshift__``、``__rshift__``、``__and__``、" +"``__xor__``、``__or__`` 和 ``__pow__``" #: ../../library/unittest.mock.rst:2030 msgid "" @@ -2325,26 +2323,28 @@ msgid "" "Different versions of Python are inconsistent about applying this rule. The " "supported protocol methods should work with all supported versions of Python." msgstr "" +"魔術方法\\ *應該*\\ 在類別而不是實例上被查找。不同版本的 Python 對於這條規則" +"的適用並不一致。支援的協定方法應適用於所有支援的 Python 版本。" #: ../../library/unittest.mock.rst:2180 msgid "" "The function is basically hooked up to the class, but each ``Mock`` instance " "is kept isolated from the others." -msgstr "" +msgstr "該函式基本上與類別掛鉤,但每個 ``Mock`` 實例都與其他實例保持隔離。" #: ../../library/unittest.mock.rst:2185 msgid "Helpers" -msgstr "" +msgstr "輔助函式" #: ../../library/unittest.mock.rst:2188 msgid "sentinel" -msgstr "" +msgstr "sentinel(哨兵)" #: ../../library/unittest.mock.rst:2192 msgid "" "The ``sentinel`` object provides a convenient way of providing unique " "objects for your tests." -msgstr "" +msgstr "``哨兵``\\ 物件提供了一種為你的測試提供獨特物件的便利方式。" #: ../../library/unittest.mock.rst:2195 msgid "" @@ -2352,12 +2352,16 @@ msgid "" "same attribute will always return the same object. The objects returned have " "a sensible repr so that test failure messages are readable." msgstr "" +"當你使用名稱存取屬性時,屬性會根據需要被建立。存取相同的屬性將始終回傳相同的" +"物件。回傳的物件會具有合適的 repr,讓測試失敗的訊息是可閱讀的。" #: ../../library/unittest.mock.rst:2199 msgid "" "The ``sentinel`` attributes now preserve their identity when they are :mod:" "`copied ` or :mod:`pickled `." msgstr "" +"``哨兵``\\ 屬性現在當被\\ :mod:`複製 `\\ 或\\ :mod:`序列化 `\\ " +"時會保留其識別性。" #: ../../library/unittest.mock.rst:2203 msgid "" @@ -2366,12 +2370,16 @@ msgid "" "sentinel objects to test this. :data:`sentinel` provides a convenient way of " "creating and testing the identity of objects like this." msgstr "" +"在測試時,有時你需要測試特定物件是否作為引數被傳遞給另一個方法或回傳。建立命" +"名的哨兵物件來測試這一點是常見的。:data:`sentinel` 提供了一種此類建立和測試物" +"件識別性的便利方式。" #: ../../library/unittest.mock.rst:2208 msgid "" "In this example we monkey patch ``method`` to return ``sentinel." "some_object``:" msgstr "" +"在這個例子中,我們 monkey patch ``method`` 以回傳 ``sentinel.some_object``:" #: ../../library/unittest.mock.rst:2220 msgid "DEFAULT" @@ -2383,10 +2391,13 @@ msgid "" "DEFAULT``). It can be used by :attr:`~Mock.side_effect` functions to " "indicate that the normal return value should be used." msgstr "" +":data:`DEFAULT` 物件是一個預先建立的哨兵(實際上是 ``sentinel." +"DEFAULT``\\ )。它可以被 :attr:`~Mock.side_effect` 函式使用來表示正常的回傳值" +"應該被使用。" #: ../../library/unittest.mock.rst:2231 msgid "call" -msgstr "" +msgstr "call" #: ../../library/unittest.mock.rst:2235 msgid "" @@ -2395,12 +2406,18 @@ msgid "" "mock_calls` and :attr:`~Mock.method_calls`. :func:`call` can also be used " "with :meth:`~Mock.assert_has_calls`." msgstr "" +"與 :attr:`~Mock.call_args`、:attr:`~Mock.call_args_list`、:attr:`~Mock." +"mock_calls` 和 :attr:`~Mock.method_calls` 相比,:func:`call` 是一個用於進行更" +"簡單的斷言的輔助物件。:func:`call` 也可以與 :meth:`~Mock.assert_has_calls` 一" +"起使用。" #: ../../library/unittest.mock.rst:2248 msgid "" "For a call object that represents multiple calls, :meth:`call_list` returns " "a list of all the intermediate calls as well as the final call." msgstr "" +"對於表示多個呼叫的 call 物件,:meth:`call_list` 回傳所有中間呼叫以及最終呼叫" +"的串列。" #: ../../library/unittest.mock.rst:2252 msgid "" @@ -2409,12 +2426,15 @@ msgid "" "results in multiple entries in :attr:`~Mock.mock_calls` on a mock. Manually " "constructing the sequence of calls can be tedious." msgstr "" +"``call_list`` 在對「鍊接呼叫 (chained calls)」進行斷言時特別有用。鍊接呼叫是" +"在單行程式碼進行的多次呼叫。這會導致 mock 上的 :attr:`~Mock.mock_calls` 中出" +"現多個項目。手動建構呼叫序列會相當單調乏味。" #: ../../library/unittest.mock.rst:2257 msgid "" ":meth:`~call.call_list` can construct the sequence of calls from the same " "chained call:" -msgstr "" +msgstr ":meth:`~call.call_list` 可以從同一個鍊接呼叫建構呼叫序列:" #: ../../library/unittest.mock.rst:2274 msgid "" @@ -2425,6 +2445,10 @@ msgid "" "call_args_list` and :attr:`Mock.mock_calls` attributes can be introspected " "to get at the individual arguments they contain." msgstr "" +"取決於它的建構方式,一個 ``call`` 物件會是(位置引數, 關鍵字引數)的元組,或" +"是 (名稱, 位置引數, 關鍵字引數) 的元組。當你自己建構它們時,這並不是那麼有" +"趣,但是 :attr:`Mock.call_args`、:attr:`Mock.call_args_list` 和 :attr:`Mock." +"mock_calls` 屬性中的 ``call`` 物件可以被內省以取得它們包含的各個引數。" #: ../../library/unittest.mock.rst:2281 msgid "" @@ -2434,6 +2458,9 @@ msgid "" "construct yourself, are three-tuples of (name, positional args, keyword " "args)." msgstr "" +":attr:`Mock.call_args` 和 :attr:`Mock.call_args_list` 中的 ``call`` 物件是" +"(位置引數, 關鍵字引數)的二元組,而 :attr:`Mock.mock_calls`中的 ``call`` 物" +"件以及你自己建立的 ``call`` 物件是(名稱, 位置引數, 關鍵字引數)的三元組。" #: ../../library/unittest.mock.rst:2286 msgid "" @@ -2442,6 +2469,8 @@ msgid "" "tuple (an empty tuple if there are no positional arguments) and the keyword " "arguments are a dictionary:" msgstr "" +"你可以利用它們作為元組的特性來提取單個引數,以進行更複雜的內省和斷言。位置引" +"數是一個元組(如果沒有位置引數則為空元組),關鍵字引數是一個字典:" #: ../../library/unittest.mock.rst:2319 msgid "create_autospec" @@ -2452,18 +2481,22 @@ msgid "" "Create a mock object using another object as a spec. Attributes on the mock " "will use the corresponding attribute on the *spec* object as their spec." msgstr "" +"使用另一個物件作為規格建立一個 mock 物件。Mock 上的屬性將使用 *spec* 物件上的" +"對應屬性作為其規格。" #: ../../library/unittest.mock.rst:2327 msgid "" "Functions or methods being mocked will have their arguments checked to " "ensure that they are called with the correct signature." -msgstr "" +msgstr "被 mock 的函式或方法將檢查其引數,以確保他們被使用正確的簽名來呼叫。" #: ../../library/unittest.mock.rst:2330 msgid "" "If *spec_set* is ``True`` then attempting to set attributes that don't exist " "on the spec object will raise an :exc:`AttributeError`." msgstr "" +"如果 *spec_set* 為 ``True``,則嘗試設定規格物件上不存在的屬性將引發 :exc:" +"`AttributeError`。" #: ../../library/unittest.mock.rst:2333 msgid "" @@ -2472,24 +2505,33 @@ msgid "" "an instance object by passing ``instance=True``. The returned mock will only " "be callable if instances of the mock are callable." msgstr "" +"如果一個類別作為規格使用,則 mock(該類別的實例)的回傳值將具有相同的規格。你" +"可以透過傳遞 ``instance=True`` 來使用一個類別作為一個實例物件的規格。只有當 " +"mock 的實例是可呼叫物件時,回傳的 mock 才會是可呼叫物件。" #: ../../library/unittest.mock.rst:2338 msgid "" ":func:`create_autospec` also takes arbitrary keyword arguments that are " "passed to the constructor of the created mock." msgstr "" +":func:`create_autospec` 也接受任意的關鍵字引數,這些引數會傳遞給已建立的 " +"mock 的建構函式。" #: ../../library/unittest.mock.rst:2341 msgid "" "See :ref:`auto-speccing` for examples of how to use auto-speccing with :func:" "`create_autospec` and the *autospec* argument to :func:`patch`." msgstr "" +"請參閱 :ref:`auto-speccing` 以得知如何以 :func:`create_autospec` 使用自動規格" +"以及如何在 :func:`patch` 中使用 *autospec* 引數的範例。" #: ../../library/unittest.mock.rst:2347 msgid "" ":func:`create_autospec` now returns an :class:`AsyncMock` if the target is " "an async function." msgstr "" +"如果目標是一個非同步函式,:func:`create_autospec` 現在會回傳一個 :class:" +"`AsyncMock`。" #: ../../library/unittest.mock.rst:2352 msgid "ANY" @@ -2502,6 +2544,8 @@ msgid "" "pull them individually out of :attr:`~Mock.call_args` and make more complex " "assertions on them." msgstr "" +"有時你可能需要對 mock 的呼叫中的\\ *某些*\\ 引數進行斷言,但你不在意其他的某" +"些引數,或想將它們單獨從 :attr:`~Mock.call_args` 中取出並進行更加複雜的斷言。" #: ../../library/unittest.mock.rst:2361 msgid "" @@ -2509,12 +2553,16 @@ msgid "" "*everything*. Calls to :meth:`~Mock.assert_called_with` and :meth:`~Mock." "assert_called_once_with` will then succeed no matter what was passed in." msgstr "" +"要忽略某些引數,你可以傳入對\\ *所有物件*\\ 來說都相等的物件。那麼無論傳入什" +"麼內容,對 :meth:`~Mock.assert_used_with` 和 :meth:`~Mock." +"assert_used_once_with` 的呼叫都會成功。" #: ../../library/unittest.mock.rst:2370 msgid "" ":data:`ANY` can also be used in comparisons with call lists like :attr:" "`~Mock.mock_calls`:" msgstr "" +":data:`ANY` 也可以用來與呼叫串列進行比較,例如 :attr:`~Mock.mock_calls`:" #: ../../library/unittest.mock.rst:2380 msgid "" @@ -2534,6 +2582,10 @@ msgid "" "filtering, or need to switch it off for diagnostic purposes, then set ``mock." "FILTER_DIR = False``." msgstr "" +":data:`FILTER_DIR` 是一個模組級別的變數,用於控制 mock 物件回應 :func:`dir`的" +"方式。其預設值為 ``True``,它使用以下描述的過濾方式來只顯示有用的成員。如果你" +"不喜歡這個過濾方式,或由於診斷意圖而需要將其關閉,請設定 ``mock.FILTER_DIR = " +"False``。" #: ../../library/unittest.mock.rst:2401 msgid "" @@ -2543,6 +2595,9 @@ msgid "" "attributes from the original are shown, even if they haven't been accessed " "yet:" msgstr "" +"當過濾方式開啟時,``dir(some_mock)`` 僅會顯示有用的屬性,並將包括通常不會顯示" +"的任何動態建立的屬性。如果 mock 是使用 *spec*\\ (或 *autospec*\\ )來建立" +"的,那麼源頭的所有屬性都會顯示,即使它們尚未被存取:" #: ../../library/unittest.mock.rst:2428 msgid "" @@ -2552,6 +2607,9 @@ msgid "" "dislike this behaviour you can switch it off by setting the module level " "switch :data:`FILTER_DIR`:" msgstr "" +"許多不是很有用的(對 :class:`Mock` 來說是私有的,而不是被 mock 的東西)底線和" +"雙底線前綴屬性已從在 :class:`Mock` 上呼叫 :func:`dir` 的結果中濾除。如果你不" +"喜歡這種特性,可以透過設定模組級別開關 :data:`FILTER_DIR` 來將其關閉:" #: ../../library/unittest.mock.rst:2449 msgid "" @@ -2559,6 +2617,9 @@ msgid "" "``dir(type(my_mock))`` (type members) to bypass the filtering irrespective " "of :const:`mock.FILTER_DIR`." msgstr "" +"或者,你可以只使用 ``vars(my_mock)``\\ (實例成員)和 " +"``dir(type(my_mock))``\\ (型別成員)來略過過濾,而不考慮 :const:`mock." +"FILTER_DIR`。" #: ../../library/unittest.mock.rst:2455 msgid "mock_open" @@ -2569,6 +2630,8 @@ msgid "" "A helper function to create a mock to replace the use of :func:`open`. It " "works for :func:`open` called directly or used as a context manager." msgstr "" +"用於建立取代 :func:`open` 用途的 mock 的輔助函式。它適用於直接呼叫或用作情境" +"管理器的 :func:`open`。" #: ../../library/unittest.mock.rst:2462 msgid "" @@ -2576,6 +2639,8 @@ msgid "" "default) then a :class:`MagicMock` will be created for you, with the API " "limited to methods or attributes available on standard file handles." msgstr "" +"*mock* 引數是要配置的 mock 物件。如果其為 ``None``\\ (預設值),那麼就會為你" +"建立一個 :class:`MagicMock`,其 API 限制在標準檔案處理上可用的方法或屬性。" #: ../../library/unittest.mock.rst:2466 msgid "" @@ -2589,6 +2654,13 @@ msgid "" "in-memory filesystem packages on `PyPI `_ can offer a " "realistic filesystem for testing." msgstr "" +"*read_data* 是檔案處理方法 :meth:`~io.IOBase.read`、:meth:`~io.IOBase." +"readline` 和 :meth:`~io.IOBase.readlines` 的回傳字串。對這些方法的呼叫將從 " +"*read_data* 取得資料,直到資料耗盡。對這些方法的 mock 非常單純:每次呼叫 " +"*mock* 時,*read_data* 都會倒回到起點。如果你需要對提供給測試程式碼的資料進" +"行更多控制,你會需要自行客製化這個 mock。如果這樣還不夠,`PyPI `_ 上的其中一個記憶體內檔案系統 (in-memory filesystem) 套件 可以提供用於" +"測試的真實檔案系統。" #: ../../library/unittest.mock.rst:2476 msgid "" @@ -2596,22 +2668,29 @@ msgid "" "The mock of :meth:`~io.IOBase.read` changed to consume *read_data* rather " "than returning it on each call." msgstr "" +"新增對 :meth:`~io.IOBase.readline` 和 :meth:`~io.IOBase.readlines` 的支援。:" +"meth:`~io.IOBase.read` 的 mock 更改為消耗 *read_data* 而不是在每次呼叫時" +"回傳它。" #: ../../library/unittest.mock.rst:2481 msgid "*read_data* is now reset on each call to the *mock*." -msgstr "" +msgstr "現在,每次呼叫 *mock* 時都會重置 *read_data*。" #: ../../library/unittest.mock.rst:2484 msgid "" "Added :meth:`~container.__iter__` to implementation so that iteration (such " "as in for loops) correctly consumes *read_data*." msgstr "" +"新增 :meth:`~container.__iter__` 到實作中,以便使疊代(例如在 for 迴圈中)正" +"確地消耗 *read_data*。" #: ../../library/unittest.mock.rst:2488 msgid "" "Using :func:`open` as a context manager is a great way to ensure your file " "handles are closed properly and is becoming common::" msgstr "" +"使用 :func:`open` 作為情境管理器是確保檔案處理正確關閉的好方式,且這種方式正" +"在變得普遍: ::" #: ../../library/unittest.mock.rst:2494 msgid "" @@ -2619,20 +2698,25 @@ msgid "" "*returned object* that is used as a context manager (and has :meth:`~object." "__enter__` and :meth:`~object.__exit__` called)." msgstr "" +"問題是,即使你 mock 了對 :func:`open` 的呼叫,它也是作為情境管理器使用的\\ *" +"回傳物件*\\ (且其 :meth:`~object.__enter__` 和 :meth:`~ object.__exit__` 已" +"被呼叫)。" #: ../../library/unittest.mock.rst:2498 msgid "" "Mocking context managers with a :class:`MagicMock` is common enough and " "fiddly enough that a helper function is useful. ::" msgstr "" +"使用 :class:`MagicMock` mock 情境管理器相當常見並且精細,因此輔助函式就非常有" +"用: ::" #: ../../library/unittest.mock.rst:2515 msgid "And for reading files::" -msgstr "" +msgstr "以及讀取檔案: ::" #: ../../library/unittest.mock.rst:2528 msgid "Autospeccing" -msgstr "" +msgstr "Autospeccing(自動規格)" #: ../../library/unittest.mock.rst:2530 msgid "" @@ -2643,10 +2727,14 @@ msgid "" "methods have the same call signature as the original so they raise a :exc:" "`TypeError` if they are called incorrectly." msgstr "" +"自動規格以 mock 現有的 :attr:`spec` 功能作為基礎。它將 mock 的 api 限制為原始" +"物件(規格)的 api,但它是遞迴的(惰性 (lazily) 實現),因此 mock 的屬性僅具" +"有與規格的屬性相同的 api。此外,被 mock 的函式/方法具有與原始的函式/方法相同" +"的呼叫簽名,因此如果它們被不正確地呼叫,就會引發 :exc:`TypeError`。" #: ../../library/unittest.mock.rst:2537 msgid "Before I explain how auto-speccing works, here's why it is needed." -msgstr "" +msgstr "在解釋自動規格如何運作之前,我們先解釋為什麼需要它。" #: ../../library/unittest.mock.rst:2539 msgid "" @@ -2655,6 +2743,9 @@ msgid "" "these flaws is specific to the :class:`Mock` api and the other is a more " "general problem with using mock objects." msgstr "" +":class:`Mock` 是一個非常強大且靈活的物件,但是當用於從被測試的系統中 mock " +"out 物件時,它有兩個缺陷。其中一個缺陷是 :class:`Mock` api 特有的,另一個缺陷" +"是使用 mock 物件時出現的更普遍的問題。" #: ../../library/unittest.mock.rst:2544 msgid "" @@ -2662,6 +2753,8 @@ msgid "" "methods that are extremely handy: :meth:`~Mock.assert_called_with` and :meth:" "`~Mock.assert_called_once_with`." msgstr "" +"首先是 :class:`Mock` 特有的問題。:class:`Mock` 有兩個非常方便的斷言方法::" +"meth:`~Mock.assert_used_with` 和 :meth:`~Mock.assert_used_once_with`。" #: ../../library/unittest.mock.rst:2557 msgid "" @@ -2669,10 +2762,12 @@ msgid "" "with arbitrary arguments, if you misspell one of these assert methods then " "your assertion is gone:" msgstr "" +"因為 mock 會根據需要自動建立屬性,並允許你使用任意引數呼叫它們,所以如果你拼" +"錯了其中一個斷言方法,那麼你的斷言就不見了:" #: ../../library/unittest.mock.rst:2567 msgid "Your tests can pass silently and incorrectly because of the typo." -msgstr "" +msgstr "由於拼字錯誤,你的測試可能會安靜且錯誤地通過。" #: ../../library/unittest.mock.rst:2569 msgid "" @@ -2681,6 +2776,9 @@ msgid "" "*old api* but uses mocks instead of the real objects will still pass. This " "means your tests can all pass even though your code is broken." msgstr "" +"第二個問題對於 mock 來說更為普遍。如果你重構某些程式碼、重新命名成員等等,則" +"對任何仍然使用\\ *舊 api* 但使用 mock 而不是真實物件的程式碼的測試仍然會通" +"過。這意味著即使你的程式碼已經壞了,你的測試也可以全部通過。" #: ../../library/unittest.mock.rst:2574 msgid "" @@ -2689,6 +2787,8 @@ msgid "" "you don't test how your units are \"wired together\" there is still lots of " "room for bugs that tests might have caught." msgstr "" +"謹記這是你需要有整合測試和單元測試的另一個原因。單獨測試所有內容都很好,但如" +"果你不測試你的單元是如何「連接在一起」的,那麼測試還是有機會發現很多錯誤。" #: ../../library/unittest.mock.rst:2579 msgid "" @@ -2696,12 +2796,15 @@ msgid "" "If you use a class or instance as the :attr:`spec` for a mock then you can " "only access attributes on the mock that exist on the real class:" msgstr "" +":mod:`mock` 已經提供了一個功能來幫助解決這個問題,其稱為 speccing。如果你使用" +"類別或實例作為 mock 的 :attr:`spec`,那麼你在 mock 上只能存取真實類別中存在的" +"屬性:" #: ../../library/unittest.mock.rst:2590 msgid "" "The spec only applies to the mock itself, so we still have the same issue " "with any methods on the mock:" -msgstr "" +msgstr "該規格僅適用於 mock 本身,因此在 mock 上的任何方法仍然有相同的問題:" #: ../../library/unittest.mock.rst:2599 msgid "" @@ -2714,10 +2817,16 @@ msgid "" "complex or deeply nested objects (like modules that import modules that " "import modules) without a big performance hit." msgstr "" +"自動規格解決了這個問題。你可以將 ``autospec=True`` 傳遞給 :func:`patch` / :" +"func:`patch.object` 或使用 :func:`create_autospec` 函式建立帶有規格的 mock。" +"如果你對 :func:`patch` 使用 ``autospec=True`` 引數,則被取代的物件將作為規格" +"物件使用。因為規格是「惰性」完成的(規格是在 mock 被存取時作為屬性被建立" +"的),所以你可以將它與非常複雜或深度巢狀使用的物件(例如連續引用的模組)一起" +"使用,而不會過於影響性能。" #: ../../library/unittest.mock.rst:2608 msgid "Here's an example of it in use::" -msgstr "" +msgstr "這是一個正在使用的例子: ::" #: ../../library/unittest.mock.rst:2618 msgid "" @@ -2725,12 +2834,15 @@ msgid "" "Request` takes two arguments in the constructor (one of which is *self*). " "Here's what happens if we try to call it incorrectly::" msgstr "" +"你可以看到 :class:`request.Request` 有一個規格。:class:`request.Request` 在建" +"構函式中接受兩個引數(其中之一是 *self*\\ )。如果我們錯誤地呼叫它,會發生以" +"下情況: ::" #: ../../library/unittest.mock.rst:2627 msgid "" "The spec also applies to instantiated classes (i.e. the return value of " "specced mocks)::" -msgstr "" +msgstr "此規格也適用於實例化的類別(即有規格的 mock 的回傳值): ::" #: ../../library/unittest.mock.rst:2634 msgid "" @@ -2739,6 +2851,9 @@ msgid "" "mock. With the spec in place any typos in our asserts will raise the correct " "error::" msgstr "" +":class:`Request` 物件不是可呼叫物件,因此實例化我們 mock out 的 :class:" +"`request.Request` 的回傳值是不可呼叫的 mock。規格到位後,斷言中的任何拼字錯誤" +"都會引發正確的錯誤: ::" #: ../../library/unittest.mock.rst:2646 msgid "" @@ -2746,12 +2861,16 @@ msgid "" "existing :func:`patch` calls and then be protected against bugs due to typos " "and api changes." msgstr "" +"在許多情況下,你只需要將 ``autospec=True`` 新增至現有的 :func:`patch` 呼叫" +"中,然後就可以防止因拼字錯誤和 api 變更而導致的錯誤。" #: ../../library/unittest.mock.rst:2650 msgid "" "As well as using *autospec* through :func:`patch` there is a :func:" "`create_autospec` for creating autospecced mocks directly:" msgstr "" +"除了透過 :func:`patch` 使用 *autospec* 之外,還有一個 :func:" +"`create_autospec` 用於直接建立有自動規格的 mock:" #: ../../library/unittest.mock.rst:2658 msgid "" @@ -2764,6 +2883,11 @@ msgid "" "be able to use autospec. On the other hand it is much better to design your " "objects so that introspection is safe [#]_." msgstr "" +"然而,這並非完全沒有限制,這就是為什麼它不是預設的行為。為了理解規格物件上有" +"哪些可用屬性,autospec 必須內省(存取屬性)規格。當你遍歷 mock 上的屬性時,原" +"始物件的對應遍歷正在默默發生。如果你的規格物件具有可以觸發程式碼執行的屬性或" +"描述器,那麼你可能無法使用 autospec。換句話說,設計你的物件讓內省是安全的 " +"[#]_ 會比較好。" #: ../../library/unittest.mock.rst:2667 msgid "" @@ -2772,6 +2896,9 @@ msgid "" "at all. *autospec* can't know about any dynamically created attributes and " "restricts the api to visible attributes. ::" msgstr "" +"一個更嚴重的問題是,在 :meth:`~object.__init__` 方法中建立實例屬性是常見的," +"而其根本不存在於類別中。*autospec* 無法知道任何動態建立的屬性,並將 api 限制" +"為可見的屬性。: ::" #: ../../library/unittest.mock.rst:2684 msgid "" @@ -2781,6 +2908,9 @@ msgid "" "you to fetch attributes that don't exist on the spec it doesn't prevent you " "setting them::" msgstr "" +"有幾種不同的方法可以解決這個問題。最簡單但可能有點煩人的方法是在建立後簡單地" +"在 mock 上設定所需的屬性。因為雖然 *autospec* 不允許你取得規格中不存在的屬" +"性,但是它不會阻止你設定它們: ::" #: ../../library/unittest.mock.rst:2695 msgid "" @@ -2789,6 +2919,9 @@ msgid "" "ensure your code only *sets* valid attributes too, but obviously it prevents " "this particular scenario:" msgstr "" +"*spec* 和 *autospec* 有一個更激進的版本,它會\\ *確實地*\\ 阻止你設定不存在的" +"屬性。如果你想確保你的程式碼僅能\\ *設定*\\ 有效的屬性,那麼這會很有用,但顯" +"然它也順便阻止了這個特殊情況:" #: ../../library/unittest.mock.rst:2708 msgid "" @@ -2798,6 +2931,9 @@ msgid "" "then providing them via class attributes (shared between instances of " "course) is faster too. e.g." msgstr "" +"解決問題的最佳方法可能是新增類別屬性作為在 :meth:`~object.__init__` 中初始化" +"的實例成員的預設值。請注意,如果你僅在 :meth:`!__init__` 中設定預設屬性,那麼" +"透過類別屬性(當然在實例之間共用)提供它們也會更快。例如:" #: ../../library/unittest.mock.rst:2719 msgid "" @@ -2809,6 +2945,11 @@ msgid "" "type, autospec doesn't use a spec for members that are set to ``None``. " "These will just be ordinary mocks (well - MagicMocks):" msgstr "" +"這就帶來了另一個問題。為稍後將成為不同型別的物件的成員提供預設值 ``None`` 是相" +"對常見的。``None`` 作為規格是無用的,因為它不允許你存取其上的\\ *任何*\\ 屬性" +"或方法。由於 ``None`` 作為規格\\ *永遠不會*\\ 有用,並且可能表示通常屬於其他" +"型別的成員,因此自動規格不會對設定為 ``None`` 的成員使用規格。這些會只是普通" +"的 mock(通常是 MagicMocks):" #: ../../library/unittest.mock.rst:2734 msgid "" @@ -2820,6 +2961,11 @@ msgid "" "the spec. Thankfully :func:`patch` supports this - you can simply pass the " "alternative object as the *autospec* argument::" msgstr "" +"如果修改正式生產 (production) 類別以新增預設值不符合你的喜好,那麼還有更多選" +"擇。其中之一就是簡單地使用實例作為規格而不是使用類別。另一種是建立一個正式生" +"產類別的子類別,並將預設值新增至子類別中,而不影響正式生產類別。這兩個都需要" +"你使用替代物件作為規格。值得慶幸的是 :func:`patch` 支援這一點 - 你可以簡單地" +"將替代物件作為 *autospec* 引數傳遞: ::" #: ../../library/unittest.mock.rst:2755 msgid "" @@ -2827,10 +2973,12 @@ msgid "" "mocked class to create a mock instance *does not* create a real instance. It " "is only attribute lookups - along with calls to :func:`dir` - that are done." msgstr "" +"這只適用於類別或已經實例化的物件。呼叫一個被 mock 的類別來建立一個 mock 實例" +"\\ *不會*\\ 建立真的實例。它僅查找屬性及對 :func:`dir` 的呼叫。" #: ../../library/unittest.mock.rst:2760 msgid "Sealing mocks" -msgstr "" +msgstr "密封 mock" #: ../../library/unittest.mock.rst:2769 msgid "" @@ -2838,6 +2986,8 @@ msgid "" "attribute of the mock being sealed or any of its attributes that are already " "mocks recursively." msgstr "" +"當存取被密封的 mock 的屬性或其任何已經遞迴 mock 的屬性時,seal 將停用 mock 的" +"自動建立。" #: ../../library/unittest.mock.rst:2772 msgid "" @@ -2845,3 +2995,5 @@ msgid "" "won't be considered in the sealing chain. This allows one to prevent seal " "from fixing part of the mock object. ::" msgstr "" +"如果將具有名稱或規格的 mock 實例指派給屬性,則不會出現在密封鏈中。這表示可藉" +"由固定 mock 物件的一部分來防止密封。: ::"