-
-
Notifications
You must be signed in to change notification settings - Fork 215
Translate mock-example #862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d94c6b
feat: to rst:1122
ken71301 1afda39
feat: to rst:1148
ken71301 65df395
feat: to rst:1342
ken71301 c89f856
fix: Inline literal start-string without end-string.
ken71301 a1024b9
Apply suggestions from code review
ken71301 a98862d
Apply suggestions from code review
ken71301 d394f77
Apply suggestions from code review
ken71301 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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-03-26 20:46+0800\n" | ||
"PO-Revision-Date: 2024-04-09 00:31+0800\n" | ||
"Last-Translator: Liang-Bo Wang <[email protected]>\n" | ||
"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" | ||
"tw)\n" | ||
|
@@ -1084,6 +1084,8 @@ msgid "" | |
"There are various reasons why you might want to subclass :class:`Mock`. One " | ||
"reason might be to add helper methods. Here's a silly example:" | ||
msgstr "" | ||
"你會想子類別化 :class:`Mock` 的原因可能有很多種。其中之一是增加輔助方法。以下" | ||
"是一個有點笨的例子:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1083 | ||
msgid "" | ||
|
@@ -1094,6 +1096,10 @@ msgid "" | |
"methods then they'll also be available on the attributes and return value " | ||
"mock of instances of your subclass." | ||
msgstr "" | ||
"``Mock`` 實例的標準行為是屬性 mock 和回傳值 mock 會與存取它們的 mock 具有相同" | ||
"的型別。這確保了 ``Mock`` 屬性是 ``Mocks``,``MagicMock`` 屬性是 " | ||
"``MagicMocks`` [#]_。因此,如果你要子類別化以新增輔助方法,那麼它們也可用於子" | ||
"類別實例的屬性 mock 和回傳值 mock。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1099 | ||
msgid "" | ||
|
@@ -1102,6 +1108,10 @@ msgid "" | |
"adaptor <https://twisted.org/documents/11.0.0/api/twisted.python.components." | ||
"html>`_. Having this applied to attributes too actually causes errors." | ||
msgstr "" | ||
"有時候這很不方便。例如,`有一個使用者 <https://code.google.com/archive/p/" | ||
"mock/issues/105>`_\\ 正在子類別化 mock 以建立 `Twisted adaptor <https://" | ||
"twisted.org/documents/11.0.0/api/twisted.python.components.html>`_。將其應用" | ||
"於屬性實際上會導致錯誤。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1105 | ||
msgid "" | ||
|
@@ -1111,24 +1121,31 @@ msgid "" | |
"signature is that it takes arbitrary keyword arguments (``**kwargs``) which " | ||
"are then passed onto the mock constructor:" | ||
msgstr "" | ||
"``Mock`` (及其各種形式)使用名為 ``_get_child_mock`` 的方法來為屬性和回傳值" | ||
"建立這些 \"子 mock\"。你可以透過置換此方法來防止你的子類別被用為屬性。其簽名" | ||
"是取用任意的關鍵字引數(``**kwargs``\\ ),然後將其傳遞給 mock 建構函式:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1122 | ||
msgid "" | ||
"An exception to this rule are the non-callable mocks. Attributes use the " | ||
"callable variant because otherwise non-callable mocks couldn't have callable " | ||
"methods." | ||
msgstr "" | ||
"此規則的例外是非可呼叫物件的 mock。屬性使用可呼叫物件的變體,否則非可呼叫物件" | ||
"的 mock 無法具有可呼叫物件的方法。" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: ../../library/unittest.mock-examples.rst:1128 | ||
msgid "Mocking imports with patch.dict" | ||
msgstr "" | ||
msgstr "使用 patch.dict 來 mock import" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1130 | ||
msgid "" | ||
"One situation where mocking can be hard is where you have a local import " | ||
"inside a function. These are harder to mock because they aren't using an " | ||
"object from the module namespace that we can patch out." | ||
msgstr "" | ||
"可能讓 mock 會很困難的一種情況是在函式內部進行區域 import。這些狀況會更難進" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"行 mock,因為它們沒有使用我們可以 patch out 的模組命名空間中的物件。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1134 | ||
msgid "" | ||
|
@@ -1139,6 +1156,11 @@ msgid "" | |
"an unconditional local import (store the module as a class or module " | ||
"attribute and only do the import on first use)." | ||
msgstr "" | ||
"一般來說,我們應該避免區域 import 的發生。有時這樣做是為了防止循環相依 " | ||
"(circular dependencies),為此\\ *通常*\\ 有更好的方式來解決問題(例如重構程式" | ||
"碼)或透過延遲 import 來防止「前期成本 (up front costs)」。這也可以透過比無條" | ||
"件的區域 import 更好的方式來解決(例如將模組儲存為類別或模組屬性,並且僅在第" | ||
"一次使用時進行 import)。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1141 | ||
msgid "" | ||
|
@@ -1149,6 +1171,10 @@ msgid "" | |
"in ``sys.modules``, so usually when you import something you get a module " | ||
"back. This need not be the case however." | ||
msgstr "" | ||
"除此之外,還有一種方法可以使用 ``mock`` 來影響 import 的結果。Import 會從 :" | ||
"data:`sys.modules` 字典中取得一個\\ *物件*。請注意,它會取得一個\\ *物件*,而" | ||
"該物件不一定是一個模組。初次 import 模組會導致模組物件被放入 ``sys.modules`` " | ||
"中,因此通常當你 import 某些東西時,你會得到一個模組。但情況並非總是如此。" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: ../../library/unittest.mock-examples.rst:1148 | ||
msgid "" | ||
|
@@ -1158,28 +1184,34 @@ msgid "" | |
"the with statement body is complete or ``patcher.stop()`` is called) then " | ||
"whatever was there previously will be restored safely." | ||
msgstr "" | ||
"這代表你可以使用 :func:`patch.dict` 來\\ *暫時*\\ 在 :data:`sys.modules` 中原" | ||
"地放置 mock。在這個 patch 存活時的任何 import 都會取得這個 mock。當 patch 完" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"成時(被裝飾的函式結束、with 陳述式主體完成或 ``patcher.stop()`` 被呼叫),那" | ||
"麼之前在 ``sys.modules`` 中的任何內容都會被安全地復原。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1154 | ||
msgid "Here's an example that mocks out the 'fooble' module." | ||
msgstr "" | ||
msgstr "下面是一個 mock out 'fooble' 模組的例子。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1166 | ||
msgid "" | ||
"As you can see the ``import fooble`` succeeds, but on exit there is no " | ||
"'fooble' left in :data:`sys.modules`." | ||
msgstr "" | ||
"如你所建,``import fooble`` 成功了,但在離開之後,:data:`sys.modules` 中就沒" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"有 'fooble' 了。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1169 | ||
msgid "This also works for the ``from module import name`` form:" | ||
msgstr "" | ||
msgstr "這也適用於 ``from module import name`` 形式:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1179 | ||
msgid "With slightly more work you can also mock package imports:" | ||
msgstr "" | ||
msgstr "透過稍微多一點的處理,你也可以 mock 套件的引入:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1192 | ||
msgid "Tracking order of calls and less verbose call assertions" | ||
msgstr "" | ||
msgstr "追蹤呼叫順序與更簡潔的呼叫斷言" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1194 | ||
msgid "" | ||
|
@@ -1188,6 +1220,9 @@ msgid "" | |
"doesn't allow you to track the order of calls between separate mock objects, " | ||
"however we can use :attr:`~Mock.mock_calls` to achieve the same effect." | ||
msgstr "" | ||
":class:`Mock` 類別可以讓你透過 :attr:`~Mock.method_calls` 屬性追蹤 mock 物件" | ||
"上方法呼叫的\\ *順序*。這不會讓你可以追蹤不同的 mock 物件之間的呼叫順序,然而" | ||
"我們可以使用 :attr:`~Mock.mock_calls` 來達到相同的效果。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1199 | ||
msgid "" | ||
|
@@ -1196,19 +1231,27 @@ msgid "" | |
"separate mocks from a parent one. Calls to those child mock will then all be " | ||
"recorded, in order, in the ``mock_calls`` of the parent:" | ||
msgstr "" | ||
"因為 mock 會在 ``mock_calls`` 中追蹤對子 mock 的呼叫,且存取 mock 的任意屬性" | ||
"會建立一個子 mock,所以我們可以從父 mock 建立不同的 mock。之後對這些子 mock " | ||
"的呼叫將依序全部記錄在父 mock 的 ``mock_calls`` 中:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1216 | ||
msgid "" | ||
"We can then assert about the calls, including the order, by comparing with " | ||
"the ``mock_calls`` attribute on the manager mock:" | ||
msgstr "" | ||
"之後我們可以透過與管理器 (manager) mock 上的 ``mock_calls`` 屬性進行比較來斷" | ||
"言呼叫的順序:" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: ../../library/unittest.mock-examples.rst:1223 | ||
msgid "" | ||
"If ``patch`` is creating, and putting in place, your mocks then you can " | ||
"attach them to a manager mock using the :meth:`~Mock.attach_mock` method. " | ||
"After attaching calls will be recorded in ``mock_calls`` of the manager. ::" | ||
msgstr "" | ||
"如果 ``patch`` 正在被建立並放置為你的 mock,那麼你可以使用 :meth:`~Mock." | ||
"attach_mock` 方法將它們附加到管理器 mock。附加之後,呼叫將被記錄在管理器的 " | ||
"``mock_calls`` 中。: ::" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1242 | ||
msgid "" | ||
|
@@ -1218,12 +1261,17 @@ msgid "" | |
"data:`call` object). If that sequence of calls are in :attr:`~Mock." | ||
"mock_calls` then the assert succeeds." | ||
msgstr "" | ||
"如果進行了多次呼叫,但你只對其中特定呼叫的序列感興趣,則可以是使用 :meth:" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"`~Mock.assert_has_calls` 方法。這需要一個呼叫串列(使用 :data:`call` 物件建" | ||
"構)。如果該呼叫序列位於 :attr:`~Mock.mock_calls` 中,則斷言成功。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1256 | ||
msgid "" | ||
"Even though the chained call ``m.one().two().three()`` aren't the only calls " | ||
"that have been made to the mock, the assert still succeeds." | ||
msgstr "" | ||
"儘管鍊接呼叫 ``m.one().two().three()`` 並不是對 mock 進行的唯一呼叫,斷言仍會" | ||
"成功。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1259 | ||
msgid "" | ||
|
@@ -1232,16 +1280,21 @@ msgid "" | |
"about the order. In this case you can pass ``any_order=True`` to " | ||
"``assert_has_calls``:" | ||
msgstr "" | ||
"有時,可能會對一個 mock 進行多次呼叫,而你只對斷言其中\\ *某些*\\ 感興趣。你" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"甚至可能不關心順序。在這種情況下,你可以將 ``any_order=True`` 傳遞給 " | ||
"``assert_has_calls``:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1271 | ||
msgid "More complex argument matching" | ||
msgstr "" | ||
msgstr "更複雜的引數匹配" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1273 | ||
msgid "" | ||
"Using the same basic concept as :data:`ANY` we can implement matchers to do " | ||
"more complex assertions on objects used as arguments to mocks." | ||
msgstr "" | ||
"使用與 :data:`ANY` 相同的基本概念,我們可以實作匹配器 (matcher),對用作對 " | ||
"mock 的引數的物件進行更複雜的斷言。" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: ../../library/unittest.mock-examples.rst:1276 | ||
msgid "" | ||
|
@@ -1252,27 +1305,32 @@ msgid "" | |
"attributes of this object then we can create a matcher that will check these " | ||
"attributes for us." | ||
msgstr "" | ||
"假設我們希望將某些物件傳遞給一個 mock,預設情況下,該 mock 會根據物件識別性" | ||
"(使用者定義類別的 Python 預設值)進行相等比較。要使用 :meth:`~Mock." | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"assert_called_with`,我們需要傳入完全相同的物件。如果我們只對該物件的某些屬性" | ||
"感興趣,那麼我們可以建立一個匹配器來為我們檢查這些屬性。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1283 | ||
msgid "" | ||
"You can see in this example how a 'standard' call to ``assert_called_with`` " | ||
"isn't sufficient:" | ||
msgstr "" | ||
"你可以在這個範例中看到對 ``assert_called_with`` 的一個「標準」呼叫是不夠的:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1298 | ||
msgid "" | ||
"A comparison function for our ``Foo`` class might look something like this:" | ||
msgstr "" | ||
msgstr "對我們的 ``Foo`` 類別的比較函式看起來可能會像這樣:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1310 | ||
msgid "" | ||
"And a matcher object that can use comparison functions like this for its " | ||
"equality operation would look something like this:" | ||
msgstr "" | ||
msgstr "可以使用這樣的比較函式進行自身相等性運算的匹配器物件會長得像這樣:" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#: ../../library/unittest.mock-examples.rst:1321 | ||
msgid "Putting all this together:" | ||
msgstr "" | ||
msgstr "把這些都放在一起:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1326 | ||
msgid "" | ||
|
@@ -1283,12 +1341,18 @@ msgid "" | |
"``assert_called_with`` passes, and if they don't an :exc:`AssertionError` is " | ||
"raised:" | ||
msgstr "" | ||
"``Matcher`` 是用我們想要比較的 ``Foo`` 物件和比較函式實例化的。在 " | ||
"``assert_called_with`` 中,``Matcher`` 相等方法將被呼叫,它將呼叫 mock 的物件" | ||
"與我們建立匹配器的物件進行比較。如果它們匹配,則 ``assert_called_with`` 會通" | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"過,如果它們不匹配,則會引發 :exc:`AssertionError`:" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1339 | ||
msgid "" | ||
"With a bit of tweaking you could have the comparison function raise the :exc:" | ||
"`AssertionError` directly and provide a more useful failure message." | ||
msgstr "" | ||
"透過一些調整,你可以讓比較函式直接引發 :exc:`AssertionError` 並提供更有用的失" | ||
"敗訊息。" | ||
|
||
#: ../../library/unittest.mock-examples.rst:1342 | ||
msgid "" | ||
|
@@ -1298,3 +1362,8 @@ msgid "" | |
"integration.match_equality <https://pyhamcrest.readthedocs.io/en/release-1.8/" | ||
"integration/#module-hamcrest.library.integration.match_equality>`_)." | ||
msgstr "" | ||
"從版本 1.5 開始,Python 測試函式庫 `PyHamcrest <https://pyhamcrest." | ||
"readthedocs.io/>`_ 提供了類似的功能,在這裡可能有用,以其相等匹配器的形式 " | ||
ken71301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"(\\ `hamcrest.library.integration.match_equality <https://pyhamcrest." | ||
"readthedocs.io/en/release-1.8/integration/#module-hamcrest.library." | ||
"integration.match_equality>`_\\ )。" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.