@@ -698,13 +698,18 @@ msgid ""
698698"the patches to all test methods on the class. A test method is identified by"
699699" methods whose names start with ``test``::"
700700msgstr ""
701+ "如果你想要为多个测试方法准备好一些补丁那么最简单的方式就是将 patch 装饰器应用到每个方法。 这在感觉上会造成不必要的重复。 对于 Python "
702+ "2.6 或更新的版本你可以使用 :func:`patch` (其所有不同的形式) 作为类装饰器。 这将把补丁应用于类中的所有测试方法。 "
703+ "测试方法是通过名称前缀为 ``test`` 来标识的::"
701704
702705#: ../../library/unittest.mock-examples.rst:685
703706msgid ""
704707"An alternative way of managing patches is to use the :ref:`start-and-stop`. "
705708"These allow you to move the patching into your ``setUp`` and ``tearDown`` "
706709"methods. ::"
707710msgstr ""
711+ "另一种管理补丁的方式是使用 :ref:`start-and-stop`。 它允许你将打补丁操作移至你的 ``setUp`` 和 ``tearDown``"
712+ " 方法中。 ::"
708713
709714#: ../../library/unittest.mock-examples.rst:702
710715msgid ""
@@ -713,10 +718,12 @@ msgid ""
713718"exception is raised in the setUp then tearDown is not called. "
714719":meth:`unittest.TestCase.addCleanup` makes this easier::"
715720msgstr ""
721+ "如果你要使用这个技巧则你必须通过调用 ``stop`` 来确保补丁被“恢复”。 这可能要比你想像的更麻烦,因为如果在 setUp 中引发了异常那么 "
722+ "tearDown 将不会被调用。 :meth:`unittest.TestCase.addCleanup` 可以做到更方便::"
716723
717724#: ../../library/unittest.mock-examples.rst:720
718725msgid "Mocking Unbound Methods"
719- msgstr ""
726+ msgstr "模拟未绑定方法 "
720727
721728#: ../../library/unittest.mock-examples.rst:722
722729msgid ""
@@ -731,6 +738,11 @@ msgid ""
731738" simple to patch out methods with a mock that having to create a real "
732739"function becomes a nuisance."
733740msgstr ""
741+ "当前在编写测试时我需要修补一个 *未绑定方法* (在类上而不是在实例上为方法打补丁)。 我需要将 self "
742+ "作为第一个参数传入因为我想对哪些对象在调用这个特定方法进行断言。 问题是这里你不能用 mock 来打补丁,因为如果你用 mock "
743+ "来替换一个未绑定方法那么当从实例中获取时它就不会成为一个已绑定方法,因而它不会获得传入的 self。 "
744+ "绕过此问题的办法是改用一个真正的函数来修补未绑定方法。 :func:`patch` 装饰器让使用 mock "
745+ "来给方法打补丁变得如此简单以至于创建一个真正的函数成为一件麻烦事。"
734746
735747#: ../../library/unittest.mock-examples.rst:733
736748msgid ""
@@ -743,12 +755,17 @@ msgid ""
743755"instance. It will have ``self`` passed in as the first argument, which is "
744756"exactly what I wanted:"
745757msgstr ""
758+ "如果将 ``autospec=True`` 传给 patch 那么它就会用一个 *真正的* 函数对象来打补丁。 "
759+ "这个函数对象具有与它所替换的函数相同的签名,但会在内部将操作委托给一个 mock。 你仍然可以通过与以前完全相同的方式来自动创建你的 mock。 "
760+ "但是这将意味着一件事,就是如果你用它来修补一个类上的非绑定方法那么如果它是从一个实例中获取则被模拟的函数将被转为已绑定方法。 传给它的第一个参数将为 "
761+ "``self``,而这真是我想要的:"
746762
747763#: ../../library/unittest.mock-examples.rst:754
748764msgid ""
749765"If we don't use ``autospec=True`` then the unbound method is patched out "
750766"with a Mock instance instead, and isn't called with ``self``."
751767msgstr ""
768+ "如果我们不使用 ``autospec=True`` 那么这个未绑定方法会改为通过一个 Mock 补丁来修补,而不是附带 ``self`` 来调用。"
752769
753770#: ../../library/unittest.mock-examples.rst:759
754771msgid "Checking multiple calls with mock"
0 commit comments