diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2013-02-02 23:23:58 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2013-02-02 23:23:58 (GMT) |
commit | 5c64df70b5b5cf8df2a651dc6630ab28e30499b8 (patch) | |
tree | 8bdb8add55c63239f018a0cc2195c8b619bf0a76 /Doc/library/unittest.mock-examples.rst | |
parent | 18b30ee88e47ef85ccc966a178bfd8f64a7f0954 (diff) | |
download | cpython-5c64df70b5b5cf8df2a651dc6630ab28e30499b8.zip cpython-5c64df70b5b5cf8df2a651dc6630ab28e30499b8.tar.gz cpython-5c64df70b5b5cf8df2a651dc6630ab28e30499b8.tar.bz2 |
Issue #17015: When it has a spec, a Mock object now inspects its signature when matching calls, so that arguments can be matched positionally or by name.
Diffstat (limited to 'Doc/library/unittest.mock-examples.rst')
-rw-r--r-- | Doc/library/unittest.mock-examples.rst | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst index 0cbb5ae..36926cd 100644 --- a/Doc/library/unittest.mock-examples.rst +++ b/Doc/library/unittest.mock-examples.rst @@ -277,6 +277,20 @@ instantiate the class in those tests. ... AttributeError: object has no attribute 'old_method' +Using a specification also enables a smarter matching of calls made to the +mock, regardless of whether some parameters were passed as positional or +named arguments:: + + >>> def f(a, b, c): pass + ... + >>> mock = Mock(spec=f) + >>> mock(1, 2, 3) + <Mock name='mock()' id='140161580456576'> + >>> mock.assert_called_with(a=1, b=2, c=3) + +If you want this smarter matching to also work with method calls on the mock, +you can use :ref:`auto-speccing <auto-speccing>`. + If you want a stronger form of specification that prevents the setting of arbitrary attributes as well as the getting of them then you can use `spec_set` instead of `spec`. |