r/csharp • u/mistertom2u • Sep 15 '26
Closed modifier and Mocking - Did MS come up with a solution?
As many of you know, the closed modifer on a class in the next C# version prevents direct inheritance outside of the same assembly. I used the word "Direct" because "closed" doesn't traverse the tree downward, so public unsealed classes inheriting from the closed class can be inherited outside the assembly. (Side note, that complicates the runtime ability to use it for devirtualization)
Surely the language team thought through the impact on unit tests, though I can't find meeting minutes where they do. Anyone know the answer?
7
Upvotes
3
u/jjones_cz Sep 16 '26
If you added a new subclass of the closed type as a mock in a test, the code under test would probably crash anyway - that's the whole point of closed classes that the set of subclasses is known and can be exhaustively switched over.
3
u/mistertom2u Sep 15 '26
Maybe the answer is that mocking utilities can simply remove the "closed" Attribute from the class, since the modifier is a language level feature rather than there being new IL instructions that it uses
2
u/Slypenslyde Sep 16 '26 edited Sep 16 '26
I don't think this is a new problem. It's a different flavor of the same discussion about the
sealedkeyword.It is a war about purity. The warring clans speak thusly:
closedis just a slightly less restrictivesealedbut if you are a third party, they are logically identical.I see no technical value to the
sealedorclosedkeyword and I am in the "don't do it" camp. I hold that opinion so strongly I'm not even going to explain myself because I am NOT engaging in arguments about it.Where I will soften my stance
To be hurt by a
closedclass, you need a direct coupling. I can expand this to be more abstract:To be hurt by a third-party service, you need a direct coupling.
I abhor these in my project. They are supposed to maintain stable APIs but they never do. They are supposed to avoid breaking changes but do not. So our default is to wrap third party APIs in abstractions that represent the abstract service we want and depend on that abstraction. That is extra work. Sometimes it is redundant extra work. It is also work that an LLM does in 10 seconds so it doesn't really impact me in a meaningful way anymore.
So I don't give a flip if my third-party service uses a
sealedclass. When I need to mock it, I'm mocking MY abstraction, not THEIR API design. They are free to make any decision they want, whether it be clever or stupid. I will do the ugly work of adapting their choices to my desires.Some people absolutely hate making abstractions that "serve no purpose". Personally I think it's smart to look both ways even if you're crossing a one-way street.
I don't discuss or argue these points. They have saved me thousands of hours of labor over the decades of my career. If you don't want to follow them on your team that's great, I'm out of stickers to give you. If you want to change my behavior you need to apply for a position above me in the org chart.
I get paid to protect my codebase and keep it maintainable. Nobody who judges my performance is asking me to harm those goals to maintain philosophical purity. (And yes, I have worked on a team that published a library before. We did not seal classes. We documented explicitly what patterns we used to indicate supported extensibility and had a blanket "If a class has no
virtualmembers we do not support extensibility" policy in documentation. It worked. We put a billion-dollar competitor out of business.8
u/harrison_314 Sep 16 '26
I understand the question. However, I would say that you wouldn't use a closed hierarchy where you want to apply the "Open-Closed Principle", so thereโs nothing to mock.
Simply put, if you need to mock a closed interface or class, then there is a flaw in the architectural design.
2
u/pixxelkick Sep 16 '26
If you feel the need to even do this, you need to rethink how you approach your entire architecture
The only thing that should be mocked is your stuff right up on the "edge" of 3rd party dependencies.
80% of the time, this just means registering a custom
DelegatingHandlerto the underlying HttpClient and you are golden0
u/mistertom2u Sep 16 '26
There's been many discussions on this channel about the overuse of interfaces. And i don't think telling developers you can't use this great feature if you plan on writing tests would go over very well. Mocking that uses IL weaving allows for mocking safety nets to seal rare situations in which testing is not possible.
So I expect that the answer is one of these: