5 ms·
After journeying through many languages since my long time with Java that ended in 2011 I have to say I have yet found a mock object framework in other language
by carrja99 10y ago
After journeying through many languages since my long time with Java that ended in 2011 I have to say I have yet found a mock object framework in other languages that match the expressiveness that Mockito did. Python's mock has the least memorable API I have ever worked with. :-(
- dom0 10y agoThis is because using Mocks in a language like Python is a bit awkward and unnatural in itself... if you look at the way tests are usually written, there is just little need for it. That's also the reason why only recently unittest.mock was added in the first place.
- samcal 10y agoCan you elaborate on this? What is the correct way to test external services that may have side effects in Python?
- bebop 10y agoI think maybe because there is no type checking in Python. This makes it more trivial to have objects that quack like a duck, without the need to explicitly create a class that conforms to an interface.
- LgWoodenBadger 10y agoFWIW, you don't do that with Mockito either, in Java land. Mockito will happily use the existing type definition of your dependency, provided that the class declaration is not final (i.e. can't be extended).
- raphw 10y agoThis is actually not a requirement anymore. Mockito 2 can mock final types and methods.
- dom0 10y agoWhen it comes to external dependencies there are mostly two ways this is dealt with: - Often there is a backend-like structure, and you got a dummy backend of sorts. Django's mail sending is a typical example of this: https://docs.djangoproject.com/en/1.10/topics/email/#in-memory-backend https://docs.djangoproject.com/en/1.10/topics/email/#in-memo... - Another commonly seen way is that there is a backend of sorts passed around and can be injected with little hassle from the tests. Often you'll see what boils down to a Mock, just written by hand, because it tends to be little hassle, and often results in quite clear testing code. Generating types on the fly using mocking and then doing somewhat awkward indirect assertions is by comparison a bit convoluted and less clear.
- samcal 10y agoAh, that's where I was confused. I would call both of those solutions mocks.
- whoisthemachine 10y agoMoq [0] in C# is much better imo. [0]: https://github.com/moq/moq4 https://github.com/moq/moq4