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…
Why a mock doesn’t work
41–50 of 77 posts
Re: Why a mock doesn’t work
#42Agreed with the articles, mocks are very tied to implementation details. I almost always prefer state-based testing: https://martinfowler.com/articles/mocksArentStubs.html As "observable state goes from a to b" is much closer to a business/functional requirement that will still/always be true, regardless of refactorings. Refactoring in codebases with state-based tests is a pleasure; in codebases with mock-based tests…
Dependency injection leads to elaborate, brittle fixtures that become more elaborate and arcane over time. And the sunk cost fallacy has people working for hours (and I’ve witnessed pairs spend two days, that’s over three man days!) trying to maintain them. I use mocks to arrange state A without going through all the business rules involved in creating state A. But you have to expose the states to do it, which can ma…
Mocks are generalized fixtures—just a reusable way to create them. And you can use them with DI. Only the method of delivery is changed, from an implicit backdoor to an explicit argument.
DI puts new requirements on production code and tests that aren't related to a specific mock, that much is true. At least if there's no automatic way to create ‘default’ dependencies.
Re: Why a mock doesn’t work
#43Agreed with the articles, mocks are very tied to implementation details. I almost always prefer state-based testing: https://martinfowler.com/articles/mocksArentStubs.html As "observable state goes from a to b" is much closer to a business/functional requirement that will still/always be true, regardless of refactorings. Refactoring in codebases with state-based tests is a pleasure; in codebases with mock-based tests…
You might also notice that this is equivalent to a function that accepts a and returns b. Which lets one use equivalence partitioning to plan the tests: https://en.wikipedia.org/wiki/Equivalence_partitioning
Except that with singletons, ‘context objects’ or similar quasi-global state, the effect might be non-isolated, and effects may be silently introduced that the calling code doesn't know about.
Re: Why a mock doesn’t work
#44Earlier quoted context omitted.
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…
"import mod" defines mod, which didn't exist in your module before.
"from mod import val" defines val, which didn't exist in your module before.
Re: Why a mock doesn’t work
#45I 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?
Since we're in this discussion, it'd be fun to change the title to “Why Python tests may fail to mock imported module functions, and a better approach to doing that”.
Re: Why a mock doesn’t work
#46Agreed with the articles, mocks are very tied to implementation details. I almost always prefer state-based testing: https://martinfowler.com/articles/mocksArentStubs.html As "observable state goes from a to b" is much closer to a business/functional requirement that will still/always be true, regardless of refactorings. Refactoring in codebases with state-based tests is a pleasure; in codebases with mock-based tests…
Re: Why a mock doesn’t work
#47Earlier quoted context omitted.
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…
Re: Why a mock doesn’t work
#48What are some other approaches?
I have followed the redux-saga pattern with success but how else should we accomplish the same goal?
Re: Why a mock doesn’t work
#49“...there are other approaches to solving the problems of isolating your product code from problematic dependencies.” What are some other approaches? I have followed the redux-saga pattern with success but how else should we accomplish the same goal?
Re: Why a mock doesn’t work
#50Why 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.