@@ -593,6 +593,9 @@ msgid ""
593593"in C, and so I couldn't just monkey-patch out the static "
594594":meth:`datetime.date.today` method."
595595msgstr ""
596+ "在某些测试中我希望模拟对 :meth:`datetime.date.today` 的调用来返回一个已知的日期,但又不想阻止被测试的代码创建新的日期对象。"
597+ " 不幸的是 :class:`datetime.date` 是用 C 语言编写的,因此我不能简单地给静态 "
598+ ":meth:`datetime.date.today` 方法打上猴子补丁。"
596599
597600#: ../../library/unittest.mock-examples.rst:608
598601msgid ""
@@ -609,6 +612,9 @@ msgid ""
609612"date. When the mock date class is called a real date will be constructed and"
610613" returned by ``side_effect``. ::"
611614msgstr ""
615+ "这里使用 :func:`patch 装饰器 <patch>` 来模拟被测试模块中的 ``date`` 类。 然后将模拟 date 类的 "
616+ ":attr:`~Mock.side_effect` 属性设为一个返回真实日期的 lambda 函数。 当模拟 date 类被调用时,将通过 "
617+ "``side_effect`` 构造并返回一个真实日期。 ::"
612618
613619#: ../../library/unittest.mock-examples.rst:626
614620msgid ""
@@ -792,6 +798,8 @@ msgid ""
792798":meth:`~Mock.assert_called_once_with` method that also asserts that the "
793799":attr:`~Mock.call_count` is one."
794800msgstr ""
801+ "如果你的 mock 只会被调用一次那么你可以使用 :meth:`~Mock.assert_called_once_with` 方法,该方法也会断言 "
802+ ":attr:`~Mock.call_count` 的值为一。"
795803
796804#: ../../library/unittest.mock-examples.rst:803
797805msgid ""
@@ -858,6 +866,9 @@ msgid ""
858866"methods for doing the assertion. Again a helper function sets this up for "
859867"me. ::"
860868msgstr ""
869+ "下面是一个使用 :attr:`~Mock.side_effect` 功能的解决方案。 如果你为 mock 提供了 ``side_effect`` "
870+ "函数那么 ``side_effect`` 将附带与该 mock 相同的参数被调用。 这样我们就有机会拷贝这些参数并将其保存起来用于之后执行断言。 "
871+ "在本例中我使用了 *另一个* mock 来保存参数以便可以使用该 mock 的方法来执行断言。 在这里辅助函数再次为我设置好了这一切。 ::"
861872
862873#: ../../library/unittest.mock-examples.rst:891
863874msgid ""
@@ -942,6 +953,9 @@ msgid ""
942953"``side_effect`` is called with the key (and in the case of ``__setitem__`` "
943954"the value too). We can also control what is returned."
944955msgstr ""
956+ "当我们的 ``MagicMock`` 的 :meth:`~object.__getitem__` 和 "
957+ ":meth:`~object.__setitem__` 方法被调用(即正常的字典访问操作)时 ``side_effect`` 将附带相应的键(对于 "
958+ "``__setitem__`` 还将附带值)被调用。 我们还可以控制返回的内容。"
945959
946960#: ../../library/unittest.mock-examples.rst:1003
947961msgid ""
0 commit comments