C
C#14mo 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
HimmDawg14mo 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
Zeo14mo ago
it does
HimmDawg
HimmDawg14mo 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
Zeo14mo 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
jalepi14mo 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
HimmDawg14mo ago
I'm a bit confused by that sentence. Can you elaborate?
Zeo
Zeo14mo 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
HimmDawg14mo 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
Zeo14mo ago
so its pointless to write tests for the proxy class?
HimmDawg
HimmDawg14mo ago
yes
Zeo
Zeo14mo ago
ah okay, then a proxy class works yeah thanks :)
Accord
Accord14mo 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.