❔ 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
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
it does
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
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
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.
I'm a bit confused by that sentence. Can you elaborate?
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
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
so its pointless to write tests for the proxy class?
yes
ah okay, then a proxy class works yeah
thanks :)
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.