-
-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot mock protected Dispose(It-IsAny-Bool) on System.ComponentModel.Component #614
Comments
I've just noticed I'm a bit behind and updated packages, and got the same outcome with the now-newest
|
@quetzalcoatl - This is a lot to take in, so please forgive me if I don't focus on the important bits right away. TL;DR: When using SetupBaseDisposeAny_DoesNotThrow_OnDisposeTrueYour main question appears to be why this test fails:
First, one thing to note that Second, and more to the point, you're trying to use Once you change The reason why you must use
SetupBaseDisposeAny_MockExceptions_DoNotOccur_DuringCollection_OnDisposeFalse
This works because NoSetup_MockExceptions_ReallyDoOccur_DuringCollection_OnDisposeFalse(Your code suggests that you expect this test to pass, but at least on my machine, it doesn't.) |
@stakx - Regards net-core vs net-framework - that is fine. Due to Oracle not having their libs built for net-core, I'm actually have a mixed runtime: Project uses net-core-2.0 libraries but it's running on net-47 runtime required by Oracle. Totally fine, they are compatible in that direction (run core-2.0 on net-4.7, not the other way :) ) Regarding Regarding ItExpr - oh god, I knew I missed something basic!! Thank you very much, I totally forgot about that. It's easy to forget when you use Protected() once in a year. I've just rechecked using ItExpr and it of course works. I'm really sorry for bothering you with an RTFM-kind of an issue! |
The latter: I assume you're well aware that relying on finalizers to set observable state is probably a pretty bad idea (since finalizers and the way they execute are rather special) and not something you should ever have to do under normal circumstances, so I opted not to look into this too deeply. It would be an interesting puzzle to solve, though.
It happens to the best of us 😜 so no worries! |
This is a kind of a follow-up to #129 and #143
I'd like to point out that the suggested workaround of .As may be somewhat reasonable, however in some cases it will work, and in some cases will not. It's simply because implementing Dispose() from IDisposable in attempt to hide existing non-virtual Dispose() is not the same as implementing virtual Dispose on from a base class like System.ComponentModel.Component.
If some 3rd party code calls Dispose() by referencing it by the base class, then it won't hit proxy-based IDisposable interface. It will call it directly from the base class, and will end up calling not-mocked protected Dispose(bool) and throw exception due to strict behavior.
In my case the 3rdparty code is from is ... the GarbageCollector. It's not really critical, GC silences those exceptions, but still it smells.
I was able to set up base Dispose(bool) for
true
andfalse
calls. However when I try to set up forIt.IsAny<bool>
then it does not work reliably in all cases.I prepared some tests below. Tests prefixed
NoSetup_
just show the original problem of having base protected Dispose(bool). Next tests try to use some Setups for that. The test calledSetupBaseDisposeAny_DoesNotThrow_OnDisposeTrue
shows the current problem. It seems that It.IsAny filter is not respected there and the setup does not work, and it ends up with exception. However an identical setup is respected in test that follows,SetupBaseDisposeAny_MockExceptions_DoNotOccur_DuringCollection_OnDisposeFalse
. I would expect both of them to pass (well, rather obvious, this setup seems simple) or both of them to fail (meaning that It.IsAny is simply not supported in Protected() ?) - but the outcome is mixed.. Finally, last tests use setups with concrete values and confirm that Protected() is able to mock the base Dispose(bool), so the core of the mechanism probably works fine and only It.IsAny glitched.For me, it seems like there's some bug out there..
During these tests I've used:
The text was updated successfully, but these errors were encountered: