Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit507c893

Browse files
committed
feat: to the end
1 parentd8524ef commit507c893

File tree

1 file changed

+101
-10
lines changed

1 file changed

+101
-10
lines changed

‎library/unittest.mock.po

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgstr ""
77
"Project-Id-Version:Python 3.12\n"
88
"Report-Msgid-Bugs-To:\n"
99
"POT-Creation-Date:2023-12-16 00:03+0000\n"
10-
"PO-Revision-Date:2024-02-21 22:04+0800\n"
10+
"PO-Revision-Date:2024-02-22 22:28+0800\n"
1111
"Last-Translator:Adrian Liaw <adrianliaw2000@gmail.com>\n"
1212
"Language-Team:Chinese - TAIWAN (https://github.com/python/python-docs-zh-"
1313
"tw)\n"
@@ -2632,13 +2632,17 @@ msgid ""
26322632
"A helper function to create a mock to replace the use of :func:`open`. It "
26332633
"works for :func:`open` called directly or used as a context manager."
26342634
msgstr""
2635+
"用於建立取代 :func:`open` 用途的 mock 的輔助函式。它適用於直接呼叫或用作情境"
2636+
"管理器的 :func:`open`。"
26352637

26362638
#:../../library/unittest.mock.rst:2462
26372639
msgid""
26382640
"The *mock* argument is the mock object to configure. If ``None`` (the "
26392641
"default) then a :class:`MagicMock` will be created for you, with the API "
26402642
"limited to methods or attributes available on standard file handles."
26412643
msgstr""
2644+
"*mock* 引數是要配置的 mock 物件。如果其 ``None``\\ (預設值),那麼就會為你建"
2645+
"立一個 :class:`MagicMock`,其 API 限制在標準檔案處理上可用的方法或屬性。"
26422646

26432647
#:../../library/unittest.mock.rst:2466
26442648
msgid""
@@ -2652,50 +2656,69 @@ msgid ""
26522656
"in-memory filesystem packages on `PyPI <https://pypi.org>`_ can offer a "
26532657
"realistic filesystem for testing."
26542658
msgstr""
2659+
"*read_data*\\ 是一個檔案處理的 :meth:`~io.IOBase.read`、:meth:`~io.IOBase."
2660+
"readline` 和 :meth:`~io.IOBase.readlines` 方法的回傳字串。對這些方法的呼叫將"
2661+
"從\\ *read_data*\\ 取得資料,直到資料耗盡。對這些方法的 mock 非常單純:每次呼"
2662+
"叫\\ *mock*\\ 時,*read_data* 都會倒回到起點。如果你需要對提供給測試程式碼的"
2663+
"資料進行更多控制,你會需要自行客製化這個 mock。如果這樣還不夠,`PyPI "
2664+
"<https://pypi.org>`_ 上的其中一個記憶體內檔案系統套件 (in-memory filesystem) "
2665+
"可以提供用於測試的真實檔案系統。"
26552666

26562667
#:../../library/unittest.mock.rst:2476
26572668
msgid""
26582669
"Added :meth:`~io.IOBase.readline` and :meth:`~io.IOBase.readlines` support. "
26592670
"The mock of :meth:`~io.IOBase.read` changed to consume *read_data* rather "
26602671
"than returning it on each call."
26612672
msgstr""
2673+
"新增對 :meth:`~io.IOBase.readline` 和 :meth:`~io.IOBase.readlines` 的支援。:"
2674+
"meth:`~io.IOBase.read` 的 mock 更改為消耗\\ *read_data*\\ 而不是在每次呼叫時"
2675+
"回傳它。"
26622676

26632677
#:../../library/unittest.mock.rst:2481
26642678
msgid"*read_data* is now reset on each call to the *mock*."
2665-
msgstr""
2679+
msgstr"現在,每次呼叫 *mock* 時都會重置 *read_data*。"
26662680

26672681
#:../../library/unittest.mock.rst:2484
26682682
msgid""
26692683
"Added :meth:`~container.__iter__` to implementation so that iteration (such "
26702684
"as in for loops) correctly consumes *read_data*."
26712685
msgstr""
2686+
"新增 :meth:`~container.__iter__` 到實作中,以便使疊代(例如在 for 迴圈中)正"
2687+
"確地消耗 *read_data*。"
26722688

26732689
#:../../library/unittest.mock.rst:2488
26742690
msgid""
26752691
"Using :func:`open` as a context manager is a great way to ensure your file "
26762692
"handles are closed properly and is becoming common::"
26772693
msgstr""
2694+
"使用 :func:`open` 作為情境管理器是確保檔案處理正確關閉的好方式,且這種方式正"
2695+
"在變得普遍: ::"
26782696

26792697
#:../../library/unittest.mock.rst:2494
26802698
msgid""
26812699
"The issue is that even if you mock out the call to :func:`open` it is the "
26822700
"*returned object* that is used as a context manager (and has :meth:`~object."
26832701
"__enter__` and :meth:`~object.__exit__` called)."
26842702
msgstr""
2703+
"問題是,即使你 mock 了對 :func:`open` 的呼叫,它也是作為情境管理器使用的 *回"
2704+
"傳物件*\\ (且其 :meth:`~object.__enter__` 和 :meth:`~ object.__exit__ ` 已被"
2705+
"呼叫)。"
26852706

26862707
#:../../library/unittest.mock.rst:2498
26872708
msgid""
26882709
"Mocking context managers with a :class:`MagicMock` is common enough and "
26892710
"fiddly enough that a helper function is useful. ::"
26902711
msgstr""
2712+
"使用 :class:`MagicMock` mock 情境管理器相當常見並且精細,因此輔助函式就非常有"
2713+
"用。: ::"
26912714

26922715
#:../../library/unittest.mock.rst:2515
26932716
msgid"And for reading files::"
2694-
msgstr""
2717+
msgstr"為了讀取檔案: ::"
26952718

26962719
#:../../library/unittest.mock.rst:2528
26972720
msgid"Autospeccing"
2698-
msgstr""
2721+
msgstr"Autospeccing (自動規格)"
26992722

27002723
#:../../library/unittest.mock.rst:2530
27012724
msgid""
@@ -2706,10 +2729,14 @@ msgid ""
27062729
"methods have the same call signature as the original so they raise a :exc:"
27072730
"`TypeError` if they are called incorrectly."
27082731
msgstr""
2732+
"自動規格以 mock 現有的 :attr:`spec` 功能作為基礎。它將 mock 的 api 限制為原始"
2733+
"物件(規格)的 api,但它是遞迴的(惰性 (lazily) 實現),因此 mock 的屬性僅具"
2734+
"有與規格的屬性相同的 api。此外,被 mock 的函式/方法具有與原始的函式/方法相同"
2735+
"的呼叫簽名,因此如果他們被不正確地呼叫,就會引發 :exc:`TypeError`。"
27092736

27102737
#:../../library/unittest.mock.rst:2537
27112738
msgid"Before I explain how auto-speccing works, here's why it is needed."
2712-
msgstr""
2739+
msgstr"在解釋自動規格如何運作之前,我們先解釋為什麼需要它。"
27132740

27142741
#:../../library/unittest.mock.rst:2539
27152742
msgid""
@@ -2718,24 +2745,31 @@ msgid ""
27182745
"these flaws is specific to the :class:`Mock` api and the other is a more "
27192746
"general problem with using mock objects."
27202747
msgstr""
2748+
":class:`Mock` 是一個非常強大且靈活的物件,但是當用於從被測試的系統中 mock "
2749+
"out 物件時,它有兩個缺陷。其中一個缺陷是 :class:`Mock` api 特有的,另一個缺陷"
2750+
"是使用 mock 物件時出現的更普遍的問題。"
27212751

27222752
#:../../library/unittest.mock.rst:2544
27232753
msgid""
27242754
"First the problem specific to :class:`Mock`. :class:`Mock` has two assert "
27252755
"methods that are extremely handy: :meth:`~Mock.assert_called_with` and :meth:"
27262756
"`~Mock.assert_called_once_with`."
27272757
msgstr""
2758+
"首先是 :class:`Mock` 特有的問題。:class:`Mock` 有兩個非常方便的斷言方法::"
2759+
"meth:`~Mock.assert_used_with` 和 :meth:`~Mock.assert_used_once_with`。"
27282760

27292761
#:../../library/unittest.mock.rst:2557
27302762
msgid""
27312763
"Because mocks auto-create attributes on demand, and allow you to call them "
27322764
"with arbitrary arguments, if you misspell one of these assert methods then "
27332765
"your assertion is gone:"
27342766
msgstr""
2767+
"因為 mock 會根據需要自動建立屬性,並允許你使用任意引數呼叫它們,所以如果你拼"
2768+
"錯了其中一個斷言方法,那麼你的斷言就不見了:"
27352769

27362770
#:../../library/unittest.mock.rst:2567
27372771
msgid"Your tests can pass silently and incorrectly because of the typo."
2738-
msgstr""
2772+
msgstr"由於拼字錯誤,你的測試可能會安靜且錯誤地通過。"
27392773

27402774
#:../../library/unittest.mock.rst:2569
27412775
msgid""
@@ -2744,6 +2778,9 @@ msgid ""
27442778
"*old api* but uses mocks instead of the real objects will still pass. This "
27452779
"means your tests can all pass even though your code is broken."
27462780
msgstr""
2781+
"第二個問題對於 mock 來說更為普遍。如果你重構某些程式碼、重新命名成員等等,則"
2782+
"任何對仍然使用\\ *舊 api*\\ 但使用 mock 而不是真實物件的程式碼地測試仍然會通"
2783+
"過。這意味著即使你的程式碼已經壞了,你的測試也可以全部通過。"
27472784

27482785
#:../../library/unittest.mock.rst:2574
27492786
msgid""
@@ -2752,19 +2789,24 @@ msgid ""
27522789
"you don't test how your units are\"wired together\" there is still lots of "
27532790
"room for bugs that tests might have caught."
27542791
msgstr""
2792+
"謹記這是你需要有整合測試和單元測試的另一個原因。單獨測試所有內容都很好,但如"
2793+
"果你不測試你的單元是如何「連接在一起」的,那麼測試還是有機會發現很多錯誤。"
27552794

27562795
#:../../library/unittest.mock.rst:2579
27572796
msgid""
27582797
":mod:`mock` already provides a feature to help with this, called speccing. "
27592798
"If you use a class or instance as the :attr:`spec` for a mock then you can "
27602799
"only access attributes on the mock that exist on the real class:"
27612800
msgstr""
2801+
":mod:`mock` 已經提供了一個功能來幫助解決這個問題,其稱為 speccing。如果你使用"
2802+
"類別或實例作為 mock 的 :attr:`spec`,那麼你在 mock 上只能存取真實類別中存在的"
2803+
"屬性:"
27622804

27632805
#:../../library/unittest.mock.rst:2590
27642806
msgid""
27652807
"The spec only applies to the mock itself, so we still have the same issue "
27662808
"with any methods on the mock:"
2767-
msgstr""
2809+
msgstr"該規格僅適用於 mock 本身,因此在 mock 上的任何方法仍然有相同的問題:"
27682810

27692811
#:../../library/unittest.mock.rst:2599
27702812
msgid""
@@ -2777,23 +2819,32 @@ msgid ""
27772819
"complex or deeply nested objects (like modules that import modules that "
27782820
"import modules) without a big performance hit."
27792821
msgstr""
2822+
"自動規格解決了這個問題。你可以將 ``autospec=True`` 傳遞給 :func:`patch` / :"
2823+
"func:`patch.object` 或使用 :func:`create_autospec` 函式建立帶有規格的 mock。"
2824+
"如果你對 :func:`patch` 使用 ``autospec=True`` 引數,則被取代的物件將作為規格"
2825+
"物件使用。因為規格是「惰性」完成的(規格是在 mock 被存取時作為屬性被建立"
2826+
"的),所以你可以將它與非常複雜或深度巢狀使用的物件(例如連續引用的模組)一起"
2827+
"使用,而不會過於影響性能。"
27802828

27812829
#:../../library/unittest.mock.rst:2608
27822830
msgid"Here's an example of it in use::"
2783-
msgstr""
2831+
msgstr"這是一個正在使用的例子: ::"
27842832

27852833
#:../../library/unittest.mock.rst:2618
27862834
msgid""
27872835
"You can see that :class:`request.Request` has a spec. :class:`request."
27882836
"Request` takes two arguments in the constructor (one of which is *self*). "
27892837
"Here's what happens if we try to call it incorrectly::"
27902838
msgstr""
2839+
"你可以看到 :class:`request.Request` 有一個規格。:class:`request.Request` 在建"
2840+
"構函式中接受兩個引數(其中之一是 *self*\\ )。如果我們錯誤地呼叫它,會發生以"
2841+
"下情況: ::"
27912842

27922843
#:../../library/unittest.mock.rst:2627
27932844
msgid""
27942845
"The spec also applies to instantiated classes (i.e. the return value of "
27952846
"specced mocks)::"
2796-
msgstr""
2847+
msgstr"此規格也適用於實例化的類別(即有規格的 mock 的回傳值): ::"
27972848

27982849
#:../../library/unittest.mock.rst:2634
27992850
msgid""
@@ -2802,19 +2853,26 @@ msgid ""
28022853
"mock. With the spec in place any typos in our asserts will raise the correct "
28032854
"error::"
28042855
msgstr""
2856+
":class:`Request` 物件不是可呼叫物件,因此實例化我們 mock out 的 :class:"
2857+
"`request.Request` 的回傳值是不可呼叫的 mock。規格到位後,斷言中的任何拼字錯誤"
2858+
"都會引發正確的錯誤: ::"
28052859

28062860
#:../../library/unittest.mock.rst:2646
28072861
msgid""
28082862
"In many cases you will just be able to add ``autospec=True`` to your "
28092863
"existing :func:`patch` calls and then be protected against bugs due to typos "
28102864
"and api changes."
28112865
msgstr""
2866+
"在許多情況下,你只需要將 ``autospec=True`` 新增至現有的 :func:`patch` 呼叫"
2867+
"中,然後就可以防止因拼字錯誤和 api 變更而導致的錯誤。"
28122868

28132869
#:../../library/unittest.mock.rst:2650
28142870
msgid""
28152871
"As well as using *autospec* through :func:`patch` there is a :func:"
28162872
"`create_autospec` for creating autospecced mocks directly:"
28172873
msgstr""
2874+
"除了透過 :func:`patch` 使用 *autospec 之外,還有一個 :func:`create_autospec` "
2875+
"用於直接建立有自動規格的 mock:"
28182876

28192877
#:../../library/unittest.mock.rst:2658
28202878
msgid""
@@ -2827,6 +2885,11 @@ msgid ""
28272885
"be able to use autospec. On the other hand it is much better to design your "
28282886
"objects so that introspection is safe [#]_."
28292887
msgstr""
2888+
"然而,這並非完全沒有限制,這就是為什麼它不是預設的行為。為了理解規格物件上有"
2889+
"哪些可用屬性,autospec 必須內省(存取屬性)規格。當你遍歷 mock 上的屬性時,原"
2890+
"始物件的對應遍歷正在默默發生。如果你的規格物件具有可以觸發程式碼執行的屬性或"
2891+
"描述器,那麼你可能無法使用 autospec。換句話說,設計你的物件讓內省是安全的 "
2892+
"[#]_ 會比較好。"
28302893

28312894
#:../../library/unittest.mock.rst:2667
28322895
msgid""
@@ -2835,6 +2898,9 @@ msgid ""
28352898
"at all. *autospec* can't know about any dynamically created attributes and "
28362899
"restricts the api to visible attributes. ::"
28372900
msgstr""
2901+
"一個更嚴重的問題是,實例屬性通常是在 :meth:`~object.__init__` 方法中建立的,"
2902+
"且根本不存在於類別中。*autospec* 無法了解任何動態建立的屬性,並將 api 限制為"
2903+
"可見的屬性。: ::"
28382904

28392905
#:../../library/unittest.mock.rst:2684
28402906
msgid""
@@ -2844,6 +2910,9 @@ msgid ""
28442910
"you to fetch attributes that don't exist on the spec it doesn't prevent you "
28452911
"setting them::"
28462912
msgstr""
2913+
"有幾種不同的方法可以解決這個問題。最簡單但不一定是最不煩人的方法是在建立後簡"
2914+
"單地在 mock 上設定所需的屬性。因為雖然 *autospec* 不允許你取得規格中不存在的"
2915+
"屬性,但是它不會阻止你設定它們: ::"
28472916

28482917
#:../../library/unittest.mock.rst:2695
28492918
msgid""
@@ -2852,6 +2921,9 @@ msgid ""
28522921
"ensure your code only *sets* valid attributes too, but obviously it prevents "
28532922
"this particular scenario:"
28542923
msgstr""
2924+
"*spec* 和 *autospec* 有一個更激進的版本,它會 *確實地* 阻止你設定不存在的屬"
2925+
"性。如果你想確保你的程式碼僅能\\ *設定*\\ 有根據的屬性,那麼這會很有用,但顯"
2926+
"然它也順便阻止了這個特殊情況:"
28552927

28562928
#:../../library/unittest.mock.rst:2708
28572929
msgid""
@@ -2861,6 +2933,9 @@ msgid ""
28612933
"then providing them via class attributes (shared between instances of "
28622934
"course) is faster too. e.g."
28632935
msgstr""
2936+
"解決問題的最佳方法可能是新增類別屬性作為在 :meth:`~object.__init__` 中初始化"
2937+
"的實例成員的預設值。請注意,如果你僅在 :meth:`!__init__` 中設定預設屬性,那麼"
2938+
"透過類別屬性(當然在實例之間共用)提供它們也會更快。例如:"
28642939

28652940
#:../../library/unittest.mock.rst:2719
28662941
msgid""
@@ -2872,6 +2947,11 @@ msgid ""
28722947
"type, autospec doesn't use a spec for members that are set to ``None``. "
28732948
"These will just be ordinary mocks (well - MagicMocks):"
28742949
msgstr""
2950+
"這就帶來了另一個問題。為稍後將成為不同型別的物件的成員提供預設值 “None” 是相"
2951+
"對常見的。``None`` 作為規格是無效的,因為它不允許你存取其上的\\ *任何*\\ 屬性"
2952+
"或方法。由於 ``None`` 作為規格\\ *永遠不會*\\ 有用,並且可能表示通常屬於其他"
2953+
"型別的成員,因此自動規格不會對設定為 ``None`` 的成員使用規格。這些會只是普通"
2954+
"的 mock(通常是 MagicMocks):"
28752955

28762956
#:../../library/unittest.mock.rst:2734
28772957
msgid""
@@ -2883,28 +2963,39 @@ msgid ""
28832963
"the spec. Thankfully :func:`patch` supports this - you can simply pass the "
28842964
"alternative object as the *autospec* argument::"
28852965
msgstr""
2966+
"如果修改正式生產 (production) 類別以新增預設值不符合你的喜好,那麼還有更多選"
2967+
"擇。其中之一就是簡單地使用實例作為規格而不是使用類別。另一種是建立一個正式生"
2968+
"產類別的子類別,並將預設值新增至子別中,而不影響正式生產類別。這兩個都需要你"
2969+
"使用替代物件作為規格。值得慶幸的是 :func:`patch` 支援這一點 - 您可以簡單地將"
2970+
"替代物件作為 *autospec* 引數傳遞: ::"
28862971

28872972
#:../../library/unittest.mock.rst:2755
28882973
msgid""
28892974
"This only applies to classes or already instantiated objects. Calling a "
28902975
"mocked class to create a mock instance *does not* create a real instance. It "
28912976
"is only attribute lookups - along with calls to :func:`dir` - that are done."
28922977
msgstr""
2978+
"這只適用於類別或已經實例化的物件。呼叫一個被 mock 的類別來建立一個 mock 實例"
2979+
"\\ *不會*\\ 建立真的實例。它僅查找屬性及呼叫已完成的 :func:`dir`。"
28932980

28942981
#:../../library/unittest.mock.rst:2760
28952982
msgid"Sealing mocks"
2896-
msgstr""
2983+
msgstr"密封 mock"
28972984

28982985
#:../../library/unittest.mock.rst:2769
28992986
msgid""
29002987
"Seal will disable the automatic creation of mocks when accessing an "
29012988
"attribute of the mock being sealed or any of its attributes that are already "
29022989
"mocks recursively."
29032990
msgstr""
2991+
"當存取被密封的 mock 的屬性或其任何已經遞迴 mock 的屬性時,seal 將停用 mock 的"
2992+
"自動建立。"
29042993

29052994
#:../../library/unittest.mock.rst:2772
29062995
msgid""
29072996
"If a mock instance with a name or a spec is assigned to an attribute it "
29082997
"won't be considered in the sealing chain. This allows one to prevent seal "
29092998
"from fixing part of the mock object. ::"
29102999
msgstr""
3000+
"如果將具有名稱或規格的 mock 實例指派給屬性,則不會出現在密封鏈中。這表示可藉"
3001+
"由固定 mock 物件的一部分來防止密封。: ::"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp