derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

[enhancement] foo('asd').return_value to differ from foo('dsa').return_value #147

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to suggest the following:

>>> m = MagicMock()
>>> m(3) == m(4)
True
>>> m.call_argument_awareness = True
>>> m(3) == m(4)
False

It really makes difference sometimes. When you know that function result 
differs from arguments (which is often the case) by doing this you could add 
strictness to your test (at my case, test without this strictness really 
doesn't make a lot of sense).

Thanks.

Original issue reported on code.google.com by k...@k-bx.com on 28 Mar 2012 at 12:12

GoogleCodeExporter commented 9 years ago
You can use side_effect to achieve what you want. For example:

    returns = {}
    def side_effect(*args, **kwargs):
        key = (args, tuple(kwargs.items()))
        return returns.setdefault(key, MagicMock())

Enclose it in a factory function if you want to reuse it with several mocks. It 
relies on hashable args - but can be modified if you need that. Fixing it in a 
general way for non-hashable argument sets without *killing* performance is 
"non-trivial".

Original comment by fuzzyman on 28 Mar 2012 at 12:57