2

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?

Alex
  • 93
  • 1
  • 6

1 Answers1

2

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.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 1
    @Alex: The best way to thank for an answer is to accept it using the tick on the left. This rewards the answerer with 15 reputation-points and tells other readers that this answer solved you problem/answered your question. – Björn Pollex Nov 16 '11 at 13:01
  • Sorry, I pressed Enter too early. To me the issue is in the paradigm inconsistency between Mox documentation and Python itself. Python is explicitly agnostic about the type of method's parameters. The Mox documentation suggests that duck-typing is not sufficient and insists on static typing wherever possible. Can you comment on this please? – Alex Nov 16 '11 at 13:04
  • maybe just mock the *interface* you expect? – Daren Thomas Nov 16 '11 at 13:35