C
C#2y ago
Zeo

❔ Unmockable

I have a class that wraps the functionality of an unmockable class. Now I want to unit test the wrapper class, however I can't because it uses the unmockable class. Is there any way around this? The most obvious way I can think of is writing an interface for the unmockable class and using this in the wrapper class, but I'm not sure if this is the best way to go around it
12 Replies
HimmDawg
HimmDawg2y ago
Does your wrapper class add additional logic to the function calls? If not, your wrapper class is actually a proxy class and those are testable if you implement an interface, as you said
Zeo
ZeoOP2y ago
it does
HimmDawg
HimmDawg2y ago
I'd then make a proxy class for that unmockable class which implements an interface. This is then testable. And your wrapper class gets this proxy class instead of the unmockable class
Zeo
ZeoOP2y ago
how would i test the proxy class then since i cant mock what it needs so i cant check whether it actually makes the correct calls
jalepi
jalepi2y ago
To mock a type, it cannot be sealed. To mock a member, it must be abstract (or from interface), or virtual. Best thing for you, is to extract an interface from the class you want to mock.
HimmDawg
HimmDawg2y ago
I'm a bit confused by that sentence. Can you elaborate?
Zeo
ZeoOP2y ago
if id create a proxy class for the unmockable class, would i need to test the proxy to make sure it actually makes calls to that unmockable class or is this unnecessary
HimmDawg
HimmDawg2y ago
I mean, you decide which calls you can make from outside. If you wanna call a function of your unmockable class through your proxy, then this proxy class must have a function that only calls the corresponding function in the unmockable class
Zeo
ZeoOP2y ago
so its pointless to write tests for the proxy class?
HimmDawg
HimmDawg2y ago
yes
Zeo
ZeoOP2y ago
ah okay, then a proxy class works yeah thanks :)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server