How to mock Pythonic way?

757 Views Asked by At

Mox mocking library allows you to be either specific or agnostic about the class you are mocking.

  1. mock = mox.CreateMock(Foo) or
  2. mock = mox.CreateMockAnything()

Mox documentation suggests to use the first way (basically check the type of the mock) wherever it is possible. Python as a dynamic language is type agnostic. The two approaches look inconsistent to me.

So, which approach to mocking is more Pythonic?

1

There are 1 best solutions below

3
On

They are not the same. From the documentation:

Some classes do not provide public interfaces; for example, they might use __getattribute__ to dynamically create their interface. For these classes, you can use a MockAnything. It does not enforce any interface, so any call your heart desires is valid. It works in the same record-replay-verify paradigm. Don't use this unless you absolutely have to! You can create a MockAnything with the CreateMockAnything method of your Mox instance like so:

In contrast, when creating a mock using CreateMock(Foo), you get an exception when an unknown method is called.