@@ -579,14 +579,14 @@ Partial mocking
579579In some tests I wanted to mock out a call to :meth: `datetime.date.today `
580580to return a known date, but I didn't want to prevent the code under test from
581581creating new date objects. Unfortunately :class: `datetime.date ` is written in C, and
582- so I couldn't just monkey-patch out the static :meth: `date.today ` method.
582+ so I couldn't just monkey-patch out the static :meth: `datetime. date.today ` method.
583583
584584I found a simple way of doing this that involved effectively wrapping the date
585585class with a mock, but passing through calls to the constructor to the real
586586class (and returning real instances).
587587
588588The :func: `patch decorator <patch> ` is used here to
589- mock out the ``date `` class in the module under test. The :attr: `side_effect `
589+ mock out the ``date `` class in the module under test. The :attr: `~Mock. side_effect `
590590attribute on the mock date class is then set to a lambda function that returns
591591a real date. When the mock date class is called a real date will be
592592constructed and returned by ``side_effect ``. ::
@@ -766,8 +766,8 @@ mock has a nice API for making assertions about how your mock objects are used.
766766 >>> mock.foo_bar.assert_called_with(' baz' , spam = ' eggs' )
767767
768768If your mock is only being called once you can use the
769- :meth: `assert_called_once_with ` method that also asserts that the
770- :attr: `call_count ` is one.
769+ :meth: `~Mock. assert_called_once_with ` method that also asserts that the
770+ :attr: `~Mock. call_count ` is one.
771771
772772 >>> mock.foo_bar.assert_called_once_with(' baz' , spam = ' eggs' )
773773 >>> mock.foo_bar()
@@ -835,7 +835,7 @@ One possibility would be for mock to copy the arguments you pass in. This
835835could then cause problems if you do assertions that rely on object identity
836836for equality.
837837
838- Here's one solution that uses the :attr: `side_effect `
838+ Here's one solution that uses the :attr: `~Mock. side_effect `
839839functionality. If you provide a ``side_effect `` function for a mock then
840840``side_effect `` will be called with the same args as the mock. This gives us an
841841opportunity to copy the arguments and store them for later assertions. In this
@@ -971,7 +971,8 @@ We can do this with :class:`MagicMock`, which will behave like a dictionary,
971971and using :data: `~Mock.side_effect ` to delegate dictionary access to a real
972972underlying dictionary that is under our control.
973973
974- When the :meth: `__getitem__ ` and :meth: `__setitem__ ` methods of our ``MagicMock `` are called
974+ When the :meth: `~object.__getitem__ ` and :meth: `~object.__setitem__ ` methods
975+ of our ``MagicMock `` are called
975976(normal dictionary access) then ``side_effect `` is called with the key (and in
976977the case of ``__setitem__ `` the value too). We can also control what is returned.
977978
0 commit comments