@@ -579,14 +579,14 @@ Partial mocking
579
579
In some tests I wanted to mock out a call to :meth: `datetime.date.today `
580
580
to return a known date, but I didn't want to prevent the code under test from
581
581
creating 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.
583
583
584
584
I found a simple way of doing this that involved effectively wrapping the date
585
585
class with a mock, but passing through calls to the constructor to the real
586
586
class (and returning real instances).
587
587
588
588
The :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 `
590
590
attribute on the mock date class is then set to a lambda function that returns
591
591
a real date. When the mock date class is called a real date will be
592
592
constructed and returned by ``side_effect ``. ::
@@ -766,8 +766,8 @@ mock has a nice API for making assertions about how your mock objects are used.
766
766
>>> mock.foo_bar.assert_called_with(' baz' , spam = ' eggs' )
767
767
768
768
If 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.
771
771
772
772
>>> mock.foo_bar.assert_called_once_with(' baz' , spam = ' eggs' )
773
773
>>> mock.foo_bar()
@@ -835,7 +835,7 @@ One possibility would be for mock to copy the arguments you pass in. This
835
835
could then cause problems if you do assertions that rely on object identity
836
836
for equality.
837
837
838
- Here's one solution that uses the :attr: `side_effect `
838
+ Here's one solution that uses the :attr: `~Mock. side_effect `
839
839
functionality. If you provide a ``side_effect `` function for a mock then
840
840
``side_effect `` will be called with the same args as the mock. This gives us an
841
841
opportunity 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,
971
971
and using :data: `~Mock.side_effect ` to delegate dictionary access to a real
972
972
underlying dictionary that is under our control.
973
973
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
975
976
(normal dictionary access) then ``side_effect `` is called with the key (and in
976
977
the case of ``__setitem__ `` the value too). We can also control what is returned.
977
978
0 commit comments