@@ -7,7 +7,7 @@ msgstr ""
7
7
"Project-Id-Version :Python 3.12\n "
8
8
"Report-Msgid-Bugs-To :\n "
9
9
"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 "
11
11
"Last-Translator :Adrian Liaw <adrianliaw2000@gmail.com>\n "
12
12
"Language-Team :Chinese - TAIWAN (https://github.com/python/python-docs-zh- "
13
13
"tw)\n "
@@ -2632,13 +2632,17 @@ msgid ""
2632
2632
"A helper function to create a mock to replace the use of :func:`open`. It "
2633
2633
"works for :func:`open` called directly or used as a context manager."
2634
2634
msgstr ""
2635
+ "用於建立取代 :func:`open` 用途的 mock 的輔助函式。它適用於直接呼叫或用作情境"
2636
+ "管理器的 :func:`open`。"
2635
2637
2636
2638
#: ../../library/unittest.mock.rst:2462
2637
2639
msgid ""
2638
2640
"The *mock* argument is the mock object to configure. If ``None`` (the "
2639
2641
"default) then a :class:`MagicMock` will be created for you, with the API "
2640
2642
"limited to methods or attributes available on standard file handles."
2641
2643
msgstr ""
2644
+ "*mock* 引數是要配置的 mock 物件。如果其 ``None``\\ (預設值),那麼就會為你建"
2645
+ "立一個 :class:`MagicMock`,其 API 限制在標準檔案處理上可用的方法或屬性。"
2642
2646
2643
2647
#: ../../library/unittest.mock.rst:2466
2644
2648
msgid ""
@@ -2652,50 +2656,69 @@ msgid ""
2652
2656
"in-memory filesystem packages on `PyPI <https://pypi.org>`_ can offer a "
2653
2657
"realistic filesystem for testing."
2654
2658
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
+ "可以提供用於測試的真實檔案系統。"
2655
2666
2656
2667
#: ../../library/unittest.mock.rst:2476
2657
2668
msgid ""
2658
2669
"Added :meth:`~io.IOBase.readline` and :meth:`~io.IOBase.readlines` support. "
2659
2670
"The mock of :meth:`~io.IOBase.read` changed to consume *read_data* rather "
2660
2671
"than returning it on each call."
2661
2672
msgstr ""
2673
+ "新增對 :meth:`~io.IOBase.readline` 和 :meth:`~io.IOBase.readlines` 的支援。:"
2674
+ "meth:`~io.IOBase.read` 的 mock 更改為消耗\\ *read_data*\\ 而不是在每次呼叫時"
2675
+ "回傳它。"
2662
2676
2663
2677
#: ../../library/unittest.mock.rst:2481
2664
2678
msgid "*read_data* is now reset on each call to the *mock*."
2665
- msgstr ""
2679
+ msgstr "現在,每次呼叫 *mock* 時都會重置 *read_data*。 "
2666
2680
2667
2681
#: ../../library/unittest.mock.rst:2484
2668
2682
msgid ""
2669
2683
"Added :meth:`~container.__iter__` to implementation so that iteration (such "
2670
2684
"as in for loops) correctly consumes *read_data*."
2671
2685
msgstr ""
2686
+ "新增 :meth:`~container.__iter__` 到實作中,以便使疊代(例如在 for 迴圈中)正"
2687
+ "確地消耗 *read_data*。"
2672
2688
2673
2689
#: ../../library/unittest.mock.rst:2488
2674
2690
msgid ""
2675
2691
"Using :func:`open` as a context manager is a great way to ensure your file "
2676
2692
"handles are closed properly and is becoming common::"
2677
2693
msgstr ""
2694
+ "使用 :func:`open` 作為情境管理器是確保檔案處理正確關閉的好方式,且這種方式正"
2695
+ "在變得普遍: ::"
2678
2696
2679
2697
#: ../../library/unittest.mock.rst:2494
2680
2698
msgid ""
2681
2699
"The issue is that even if you mock out the call to :func:`open` it is the "
2682
2700
"*returned object* that is used as a context manager (and has :meth:`~object."
2683
2701
"__enter__` and :meth:`~object.__exit__` called)."
2684
2702
msgstr ""
2703
+ "問題是,即使你 mock 了對 :func:`open` 的呼叫,它也是作為情境管理器使用的 *回"
2704
+ "傳物件*\\ (且其 :meth:`~object.__enter__` 和 :meth:`~ object.__exit__ ` 已被"
2705
+ "呼叫)。"
2685
2706
2686
2707
#: ../../library/unittest.mock.rst:2498
2687
2708
msgid ""
2688
2709
"Mocking context managers with a :class:`MagicMock` is common enough and "
2689
2710
"fiddly enough that a helper function is useful. ::"
2690
2711
msgstr ""
2712
+ "使用 :class:`MagicMock` mock 情境管理器相當常見並且精細,因此輔助函式就非常有"
2713
+ "用。: ::"
2691
2714
2692
2715
#: ../../library/unittest.mock.rst:2515
2693
2716
msgid "And for reading files::"
2694
- msgstr ""
2717
+ msgstr "為了讀取檔案: :: "
2695
2718
2696
2719
#: ../../library/unittest.mock.rst:2528
2697
2720
msgid "Autospeccing"
2698
- msgstr ""
2721
+ msgstr "Autospeccing (自動規格) "
2699
2722
2700
2723
#: ../../library/unittest.mock.rst:2530
2701
2724
msgid ""
@@ -2706,10 +2729,14 @@ msgid ""
2706
2729
"methods have the same call signature as the original so they raise a :exc:"
2707
2730
"`TypeError` if they are called incorrectly."
2708
2731
msgstr ""
2732
+ "自動規格以 mock 現有的 :attr:`spec` 功能作為基礎。它將 mock 的 api 限制為原始"
2733
+ "物件(規格)的 api,但它是遞迴的(惰性 (lazily) 實現),因此 mock 的屬性僅具"
2734
+ "有與規格的屬性相同的 api。此外,被 mock 的函式/方法具有與原始的函式/方法相同"
2735
+ "的呼叫簽名,因此如果他們被不正確地呼叫,就會引發 :exc:`TypeError`。"
2709
2736
2710
2737
#: ../../library/unittest.mock.rst:2537
2711
2738
msgid "Before I explain how auto-speccing works, here's why it is needed."
2712
- msgstr ""
2739
+ msgstr "在解釋自動規格如何運作之前,我們先解釋為什麼需要它。 "
2713
2740
2714
2741
#: ../../library/unittest.mock.rst:2539
2715
2742
msgid ""
@@ -2718,24 +2745,31 @@ msgid ""
2718
2745
"these flaws is specific to the :class:`Mock` api and the other is a more "
2719
2746
"general problem with using mock objects."
2720
2747
msgstr ""
2748
+ ":class:`Mock` 是一個非常強大且靈活的物件,但是當用於從被測試的系統中 mock "
2749
+ "out 物件時,它有兩個缺陷。其中一個缺陷是 :class:`Mock` api 特有的,另一個缺陷"
2750
+ "是使用 mock 物件時出現的更普遍的問題。"
2721
2751
2722
2752
#: ../../library/unittest.mock.rst:2544
2723
2753
msgid ""
2724
2754
"First the problem specific to :class:`Mock`. :class:`Mock` has two assert "
2725
2755
"methods that are extremely handy: :meth:`~Mock.assert_called_with` and :meth:"
2726
2756
"`~Mock.assert_called_once_with`."
2727
2757
msgstr ""
2758
+ "首先是 :class:`Mock` 特有的問題。:class:`Mock` 有兩個非常方便的斷言方法::"
2759
+ "meth:`~Mock.assert_used_with` 和 :meth:`~Mock.assert_used_once_with`。"
2728
2760
2729
2761
#: ../../library/unittest.mock.rst:2557
2730
2762
msgid ""
2731
2763
"Because mocks auto-create attributes on demand, and allow you to call them "
2732
2764
"with arbitrary arguments, if you misspell one of these assert methods then "
2733
2765
"your assertion is gone:"
2734
2766
msgstr ""
2767
+ "因為 mock 會根據需要自動建立屬性,並允許你使用任意引數呼叫它們,所以如果你拼"
2768
+ "錯了其中一個斷言方法,那麼你的斷言就不見了:"
2735
2769
2736
2770
#: ../../library/unittest.mock.rst:2567
2737
2771
msgid "Your tests can pass silently and incorrectly because of the typo."
2738
- msgstr ""
2772
+ msgstr "由於拼字錯誤,你的測試可能會安靜且錯誤地通過。 "
2739
2773
2740
2774
#: ../../library/unittest.mock.rst:2569
2741
2775
msgid ""
@@ -2744,6 +2778,9 @@ msgid ""
2744
2778
"*old api* but uses mocks instead of the real objects will still pass. This "
2745
2779
"means your tests can all pass even though your code is broken."
2746
2780
msgstr ""
2781
+ "第二個問題對於 mock 來說更為普遍。如果你重構某些程式碼、重新命名成員等等,則"
2782
+ "任何對仍然使用\\ *舊 api*\\ 但使用 mock 而不是真實物件的程式碼地測試仍然會通"
2783
+ "過。這意味著即使你的程式碼已經壞了,你的測試也可以全部通過。"
2747
2784
2748
2785
#: ../../library/unittest.mock.rst:2574
2749
2786
msgid ""
@@ -2752,19 +2789,24 @@ msgid ""
2752
2789
"you don't test how your units are\" wired together\" there is still lots of "
2753
2790
"room for bugs that tests might have caught."
2754
2791
msgstr ""
2792
+ "謹記這是你需要有整合測試和單元測試的另一個原因。單獨測試所有內容都很好,但如"
2793
+ "果你不測試你的單元是如何「連接在一起」的,那麼測試還是有機會發現很多錯誤。"
2755
2794
2756
2795
#: ../../library/unittest.mock.rst:2579
2757
2796
msgid ""
2758
2797
":mod:`mock` already provides a feature to help with this, called speccing. "
2759
2798
"If you use a class or instance as the :attr:`spec` for a mock then you can "
2760
2799
"only access attributes on the mock that exist on the real class:"
2761
2800
msgstr ""
2801
+ ":mod:`mock` 已經提供了一個功能來幫助解決這個問題,其稱為 speccing。如果你使用"
2802
+ "類別或實例作為 mock 的 :attr:`spec`,那麼你在 mock 上只能存取真實類別中存在的"
2803
+ "屬性:"
2762
2804
2763
2805
#: ../../library/unittest.mock.rst:2590
2764
2806
msgid ""
2765
2807
"The spec only applies to the mock itself, so we still have the same issue "
2766
2808
"with any methods on the mock:"
2767
- msgstr ""
2809
+ msgstr "該規格僅適用於 mock 本身,因此在 mock 上的任何方法仍然有相同的問題: "
2768
2810
2769
2811
#: ../../library/unittest.mock.rst:2599
2770
2812
msgid ""
@@ -2777,23 +2819,32 @@ msgid ""
2777
2819
"complex or deeply nested objects (like modules that import modules that "
2778
2820
"import modules) without a big performance hit."
2779
2821
msgstr ""
2822
+ "自動規格解決了這個問題。你可以將 ``autospec=True`` 傳遞給 :func:`patch` / :"
2823
+ "func:`patch.object` 或使用 :func:`create_autospec` 函式建立帶有規格的 mock。"
2824
+ "如果你對 :func:`patch` 使用 ``autospec=True`` 引數,則被取代的物件將作為規格"
2825
+ "物件使用。因為規格是「惰性」完成的(規格是在 mock 被存取時作為屬性被建立"
2826
+ "的),所以你可以將它與非常複雜或深度巢狀使用的物件(例如連續引用的模組)一起"
2827
+ "使用,而不會過於影響性能。"
2780
2828
2781
2829
#: ../../library/unittest.mock.rst:2608
2782
2830
msgid "Here's an example of it in use::"
2783
- msgstr ""
2831
+ msgstr "這是一個正在使用的例子: :: "
2784
2832
2785
2833
#: ../../library/unittest.mock.rst:2618
2786
2834
msgid ""
2787
2835
"You can see that :class:`request.Request` has a spec. :class:`request."
2788
2836
"Request` takes two arguments in the constructor (one of which is *self*). "
2789
2837
"Here's what happens if we try to call it incorrectly::"
2790
2838
msgstr ""
2839
+ "你可以看到 :class:`request.Request` 有一個規格。:class:`request.Request` 在建"
2840
+ "構函式中接受兩個引數(其中之一是 *self*\\ )。如果我們錯誤地呼叫它,會發生以"
2841
+ "下情況: ::"
2791
2842
2792
2843
#: ../../library/unittest.mock.rst:2627
2793
2844
msgid ""
2794
2845
"The spec also applies to instantiated classes (i.e. the return value of "
2795
2846
"specced mocks)::"
2796
- msgstr ""
2847
+ msgstr "此規格也適用於實例化的類別(即有規格的 mock 的回傳值): :: "
2797
2848
2798
2849
#: ../../library/unittest.mock.rst:2634
2799
2850
msgid ""
@@ -2802,19 +2853,26 @@ msgid ""
2802
2853
"mock. With the spec in place any typos in our asserts will raise the correct "
2803
2854
"error::"
2804
2855
msgstr ""
2856
+ ":class:`Request` 物件不是可呼叫物件,因此實例化我們 mock out 的 :class:"
2857
+ "`request.Request` 的回傳值是不可呼叫的 mock。規格到位後,斷言中的任何拼字錯誤"
2858
+ "都會引發正確的錯誤: ::"
2805
2859
2806
2860
#: ../../library/unittest.mock.rst:2646
2807
2861
msgid ""
2808
2862
"In many cases you will just be able to add ``autospec=True`` to your "
2809
2863
"existing :func:`patch` calls and then be protected against bugs due to typos "
2810
2864
"and api changes."
2811
2865
msgstr ""
2866
+ "在許多情況下,你只需要將 ``autospec=True`` 新增至現有的 :func:`patch` 呼叫"
2867
+ "中,然後就可以防止因拼字錯誤和 api 變更而導致的錯誤。"
2812
2868
2813
2869
#: ../../library/unittest.mock.rst:2650
2814
2870
msgid ""
2815
2871
"As well as using *autospec* through :func:`patch` there is a :func:"
2816
2872
"`create_autospec` for creating autospecced mocks directly:"
2817
2873
msgstr ""
2874
+ "除了透過 :func:`patch` 使用 *autospec 之外,還有一個 :func:`create_autospec` "
2875
+ "用於直接建立有自動規格的 mock:"
2818
2876
2819
2877
#: ../../library/unittest.mock.rst:2658
2820
2878
msgid ""
@@ -2827,6 +2885,11 @@ msgid ""
2827
2885
"be able to use autospec. On the other hand it is much better to design your "
2828
2886
"objects so that introspection is safe [#]_."
2829
2887
msgstr ""
2888
+ "然而,這並非完全沒有限制,這就是為什麼它不是預設的行為。為了理解規格物件上有"
2889
+ "哪些可用屬性,autospec 必須內省(存取屬性)規格。當你遍歷 mock 上的屬性時,原"
2890
+ "始物件的對應遍歷正在默默發生。如果你的規格物件具有可以觸發程式碼執行的屬性或"
2891
+ "描述器,那麼你可能無法使用 autospec。換句話說,設計你的物件讓內省是安全的 "
2892
+ "[#]_ 會比較好。"
2830
2893
2831
2894
#: ../../library/unittest.mock.rst:2667
2832
2895
msgid ""
@@ -2835,6 +2898,9 @@ msgid ""
2835
2898
"at all. *autospec* can't know about any dynamically created attributes and "
2836
2899
"restricts the api to visible attributes. ::"
2837
2900
msgstr ""
2901
+ "一個更嚴重的問題是,實例屬性通常是在 :meth:`~object.__init__` 方法中建立的,"
2902
+ "且根本不存在於類別中。*autospec* 無法了解任何動態建立的屬性,並將 api 限制為"
2903
+ "可見的屬性。: ::"
2838
2904
2839
2905
#: ../../library/unittest.mock.rst:2684
2840
2906
msgid ""
@@ -2844,6 +2910,9 @@ msgid ""
2844
2910
"you to fetch attributes that don't exist on the spec it doesn't prevent you "
2845
2911
"setting them::"
2846
2912
msgstr ""
2913
+ "有幾種不同的方法可以解決這個問題。最簡單但不一定是最不煩人的方法是在建立後簡"
2914
+ "單地在 mock 上設定所需的屬性。因為雖然 *autospec* 不允許你取得規格中不存在的"
2915
+ "屬性,但是它不會阻止你設定它們: ::"
2847
2916
2848
2917
#: ../../library/unittest.mock.rst:2695
2849
2918
msgid ""
@@ -2852,6 +2921,9 @@ msgid ""
2852
2921
"ensure your code only *sets* valid attributes too, but obviously it prevents "
2853
2922
"this particular scenario:"
2854
2923
msgstr ""
2924
+ "*spec* 和 *autospec* 有一個更激進的版本,它會 *確實地* 阻止你設定不存在的屬"
2925
+ "性。如果你想確保你的程式碼僅能\\ *設定*\\ 有根據的屬性,那麼這會很有用,但顯"
2926
+ "然它也順便阻止了這個特殊情況:"
2855
2927
2856
2928
#: ../../library/unittest.mock.rst:2708
2857
2929
msgid ""
@@ -2861,6 +2933,9 @@ msgid ""
2861
2933
"then providing them via class attributes (shared between instances of "
2862
2934
"course) is faster too. e.g."
2863
2935
msgstr ""
2936
+ "解決問題的最佳方法可能是新增類別屬性作為在 :meth:`~object.__init__` 中初始化"
2937
+ "的實例成員的預設值。請注意,如果你僅在 :meth:`!__init__` 中設定預設屬性,那麼"
2938
+ "透過類別屬性(當然在實例之間共用)提供它們也會更快。例如:"
2864
2939
2865
2940
#: ../../library/unittest.mock.rst:2719
2866
2941
msgid ""
@@ -2872,6 +2947,11 @@ msgid ""
2872
2947
"type, autospec doesn't use a spec for members that are set to ``None``. "
2873
2948
"These will just be ordinary mocks (well - MagicMocks):"
2874
2949
msgstr ""
2950
+ "這就帶來了另一個問題。為稍後將成為不同型別的物件的成員提供預設值 “None” 是相"
2951
+ "對常見的。``None`` 作為規格是無效的,因為它不允許你存取其上的\\ *任何*\\ 屬性"
2952
+ "或方法。由於 ``None`` 作為規格\\ *永遠不會*\\ 有用,並且可能表示通常屬於其他"
2953
+ "型別的成員,因此自動規格不會對設定為 ``None`` 的成員使用規格。這些會只是普通"
2954
+ "的 mock(通常是 MagicMocks):"
2875
2955
2876
2956
#: ../../library/unittest.mock.rst:2734
2877
2957
msgid ""
@@ -2883,28 +2963,39 @@ msgid ""
2883
2963
"the spec. Thankfully :func:`patch` supports this - you can simply pass the "
2884
2964
"alternative object as the *autospec* argument::"
2885
2965
msgstr ""
2966
+ "如果修改正式生產 (production) 類別以新增預設值不符合你的喜好,那麼還有更多選"
2967
+ "擇。其中之一就是簡單地使用實例作為規格而不是使用類別。另一種是建立一個正式生"
2968
+ "產類別的子類別,並將預設值新增至子別中,而不影響正式生產類別。這兩個都需要你"
2969
+ "使用替代物件作為規格。值得慶幸的是 :func:`patch` 支援這一點 - 您可以簡單地將"
2970
+ "替代物件作為 *autospec* 引數傳遞: ::"
2886
2971
2887
2972
#: ../../library/unittest.mock.rst:2755
2888
2973
msgid ""
2889
2974
"This only applies to classes or already instantiated objects. Calling a "
2890
2975
"mocked class to create a mock instance *does not* create a real instance. It "
2891
2976
"is only attribute lookups - along with calls to :func:`dir` - that are done."
2892
2977
msgstr ""
2978
+ "這只適用於類別或已經實例化的物件。呼叫一個被 mock 的類別來建立一個 mock 實例"
2979
+ "\\ *不會*\\ 建立真的實例。它僅查找屬性及呼叫已完成的 :func:`dir`。"
2893
2980
2894
2981
#: ../../library/unittest.mock.rst:2760
2895
2982
msgid "Sealing mocks"
2896
- msgstr ""
2983
+ msgstr "密封 mock "
2897
2984
2898
2985
#: ../../library/unittest.mock.rst:2769
2899
2986
msgid ""
2900
2987
"Seal will disable the automatic creation of mocks when accessing an "
2901
2988
"attribute of the mock being sealed or any of its attributes that are already "
2902
2989
"mocks recursively."
2903
2990
msgstr ""
2991
+ "當存取被密封的 mock 的屬性或其任何已經遞迴 mock 的屬性時,seal 將停用 mock 的"
2992
+ "自動建立。"
2904
2993
2905
2994
#: ../../library/unittest.mock.rst:2772
2906
2995
msgid ""
2907
2996
"If a mock instance with a name or a spec is assigned to an attribute it "
2908
2997
"won't be considered in the sealing chain. This allows one to prevent seal "
2909
2998
"from fixing part of the mock object. ::"
2910
2999
msgstr ""
3000
+ "如果將具有名稱或規格的 mock 實例指派給屬性,則不會出現在密封鏈中。這表示可藉"
3001
+ "由固定 mock 物件的一部分來防止密封。: ::"