Live data from Hacker News

Why a mock doesn’t work

nedbatchelder.com

31–40 of 77 posts

Re: Why a mock doesn’t work

#31
post #30

I've wrestled with this problem several times. My conclusion was mocks are just fine, but this is a wart in that there isn't "one way to do it" For the most part I can get by with one rule: always mock the module . with mock.patch('os.listdir'): will always work, even if it doesn't accomplish what you want. with mock.patch('mymodule.os.listdir') will fail if that module does not explicitly import os and instead does…

I don't understand what you mean by "will always work, even if it doesn't accomplish what you want." Mocking os.listdir will be useless if your product code's imports don't match it. How is this "one rule" to use?

Re: Why a mock doesn’t work

#32
I really don't like that the topic title is a general statement but the article under is talking strictly in python landscape as an example and not discussing the idea itself.

Mocking by itself works fine. It's a good idea that works if used correctly. Misusing it or bad usage leads to issues - duh.

I'm familiar with the narrative that anything that is too hard (to get right at the first try) in our field means that it is bad - but I don't agree with it at all. We are at the point where craftsmanship should be a good metric to distinguish medicore developers from experts..

Re: Why a mock doesn’t work

#33

If you find yourself fighting with mocks, ask yourself: is there a deeper design problem with my code? I often find that things I can't test easily have crappy design.

Couldn't agree more. I've done a few "coding dojo" sessions where my team and I start from scratch and write a new set of mock-based tests for a piece of existing code, and when it starts to get gnarly, it's always been because of an inconsistent interface, or confusing API of the code under test.

Bob Martin talks about this a lot; your UTs should be thought of as first-class clients of your objects' APIs. If something is hard to test, it's probably hard to use, or abstracted at the wrong level.

Re: Why a mock doesn’t work

#34
post #32

I really don't like that the topic title is a general statement but the article under is talking strictly in python landscape as an example and not discussing the idea itself. Mocking by itself works fine. It's a good idea that works if used correctly. Misusing it or bad usage leads to issues - duh. I'm familiar with the narrative that anything that is too hard (to get right at the first try) in our field means that…

What title would you prefer? "Your Python mock might not work, but it could still be a good idea if you do it right, and here I will explain how"? :)

I didn't mean to imply that mocking is bad. Is that what you took from it? Why would I explain how to get mocks to work if I thought people shouldn't use mocks?

Re: Why a mock doesn’t work

#35
post #34
post #32

I really don't like that the topic title is a general statement but the article under is talking strictly in python landscape as an example and not discussing the idea itself. Mocking by itself works fine. It's a good idea that works if used correctly. Misusing it or bad usage leads to issues - duh. I'm familiar with the narrative that anything that is too hard (to get right at the first try) in our field means that…

What title would you prefer? "Your Python mock might not work, but it could still be a good idea if you do it right, and here I will explain how"? :) I didn't mean to imply that mocking is bad. Is that what you took from it? Why would I explain how to get mocks to work if I thought people shouldn't use mocks?

A better question would be: "Why would I use a generic title, if I dont agree with it in my own article?"

And the answer would be: " A click-bite title"..

Re: Why a mock doesn’t work

#36
post #3
post #2

For a person who does not code very large programs in Python, this looks scary and like something that is hardly "only one way to do it" and that the most elegant solution gives you what you want. How I import things (or the libraries I depend on!) affects how I can write my tests? Really? My own Python scripts are typically single-screen in length. And one-off stuff, where they either work or don't, basically. Is th…

no, this blog post is absolutely on point and correct. it can be extremely inconvenient and complicated to get around using mocks in many situations where you want to test things, so we want to use mocks when appropriate. Then, you definitely want to mock at the most specific level possible. while mocking in a way that is specific to how modules are imported is technically "fragile", in that it is deeply dependent on…

I think they are asking a question about Python, not about mocks.

I was also very surprised Python does it this way. Normally I would expect "import" statements in a language to just rearrange the namespace and have no other effects at all. The author explains that it doesn't work that way:

> “from mod import val” means, import mod, and then do the assignment “val = mod.val”.

In other words, I would expect an import to alter a symbol table. I would not expect it to create a new variable and then perform an assignment.

Re: Why a mock doesn’t work

#37
post #34
post #32

I really don't like that the topic title is a general statement but the article under is talking strictly in python landscape as an example and not discussing the idea itself. Mocking by itself works fine. It's a good idea that works if used correctly. Misusing it or bad usage leads to issues - duh. I'm familiar with the narrative that anything that is too hard (to get right at the first try) in our field means that…

What title would you prefer? "Your Python mock might not work, but it could still be a good idea if you do it right, and here I will explain how"? :) I didn't mean to imply that mocking is bad. Is that what you took from it? Why would I explain how to get mocks to work if I thought people shouldn't use mocks?

I’ve recently learned that some actually do consider mocking harmful.

Where I work right now, there’s a really outdated and unfashionable fight over the benefit of unit testing in general. Existing engineers don’t see value in test driven development, exhaustive testing, unit testing, and mocks/spies are thrown in... and we’re a python shop. I’m utterly confused. I, too, grew concerned after reading–will I be hearing this cited/twisted as further evidence against investing in our dreadful testing situation?

Re: Why a mock doesn’t work

#38
post #35
post #34

Earlier quoted context omitted.

What title would you prefer? "Your Python mock might not work, but it could still be a good idea if you do it right, and here I will explain how"? :) I didn't mean to imply that mocking is bad. Is that what you took from it? Why would I explain how to get mocks to work if I thought people shouldn't use mocks?

A better question would be: "Why would I use a generic title, if I dont agree with it in my own article?" And the answer would be: " A click-bite title"..

I'm not sure what your objection is. People try to use mocks, and they don't work because they've mocked the wrong name. I explain this in the article. The article is about why their mock didn't work. How is this a misleading title? How is this a title I don't agree with?

Are we talking about the same piece and the same title? You seem to be under the impression that I am trying to tell people not to use mocks. Have you read the piece?

Re: Why a mock doesn’t work

#39

Why I don't mock: https://blog.metaobject.com/2014/05/why-i-don-mock.html

You're talking about mocking database calls though. In my line of work (insurance brokerage), we use lots of insurance APIs and they are sometimes very slow (+20 seconds / call) or completely down at random hours. There is simply no way around mocking those API calls if you want a fast and reliable testsuite.

> or completely down at random hours

Also describes "dinosaur payment company" API sandboxes (stage).

So we end up testing against the production API with a staff member's credit card - well if we want to deploy any time soon.

Or I guess you could mock and cross your fingers that they haven't changed the API recently without telling you. Payment APIs are the most solid, but that's a low bar considering the state of third-party APIs in the real world.

If it wasn't hard for enterprises to build and manage APIs, then Google Apigee and Mulesoft wouldn't be worth billions.

Re: Why a mock doesn’t work

#40

Why I don't mock: https://blog.metaobject.com/2014/05/why-i-don-mock.html

You're talking about mocking database calls though. In my line of work (insurance brokerage), we use lots of insurance APIs and they are sometimes very slow (+20 seconds / call) or completely down at random hours. There is simply no way around mocking those API calls if you want a fast and reliable testsuite.

Yes we can mock databases and queries in C#

var oldMen = from p in context.Persons where p.Sex == “M” && p.Age > 65;

“context” could be an Entity Framework context that would generate sql at runtime, an IMongoQueryable that would generate MongoQuery at runtime or an in memory List that would generate the equivalent foreach loop with an if condition (overly simplified) while running your tests.

Post reply on HN