Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95347

Mock object but also assert method calls?

Newsgroups comp.lang.python
Date 2015-08-13 08:58 -0700
Message-ID <509adc48-996d-44b0-abf1-e15ee387023b@googlegroups.com> (permalink)
Subject Mock object but also assert method calls?
From Thomas Lehmann <thomas.lehmann.private@googlemail.com>

Show all headers | View raw


Hi,

How about asserting that test2 of class Bar is called?
Of course I can do a patch for a concrete method but
I was looking for something like:

    mocked_object.assert_method_called_with(name="test2", "hello")

If find following totally different to the normal API which
is provided by the mock library:

assert call().test2("hello") in mocked_objects.mock_calls

Is there a better way?

APPENDIX:
Foo delegates calls to Bar.

[code]
    from mock import patch
    with patch("__main__.Bar") as mocked_object:
        foo = Foo()
        foo.test1()
        foo.test2("hello")
        print(mocked_object.mock_calls)
        # print all calls (c'tor as well as normal methods)
        for name, args, kwargs in mocked_object.mock_calls:
            print(name, args, kwargs)
[/code]

Generates:

<class '__main__.Bar'>
test1: Foo has been called
test2: Foo has been called with value hello
[call(), call().test1(), call().test2('hello')]
('', (), {})
('().test1', (), {})
('().test2', ('hello',), {})

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Mock object but also assert method calls? Thomas Lehmann <thomas.lehmann.private@googlemail.com> - 2015-08-13 08:58 -0700
  Re: Mock object but also assert method calls? Ben Finney <ben+python@benfinney.id.au> - 2015-08-14 07:21 +1000
    Re: Mock object but also assert method calls? Steven D'Aprano <steve@pearwood.info> - 2015-08-14 12:53 +1000
      Re: Mock object but also assert method calls? Thomas Lehmann <thomas.lehmann.private@googlemail.com> - 2015-08-15 04:07 -0700

csiph-web