Question

I want to unit test a MVP presenter class . but its interaction to the service is not through interface ( it uses a concrete class ) . now I wonder is there anyway to mock service class without being have to change the structure ?

Was it helpful?

Solution

The usual way of mocking in C#/VB.NET is by overriding virtual methods with the implementation you require for a particular scenario. For that reason, an interface is the approach most commonly used as all its methods are abstract.

Is there a reason why you are reluctant to change the service dependency to an interface? An alternative is making the methods you want to test virtual, so you can override them in your mocks, but I would not recommend that, as it is a bit dirty.

The whole point of unit testing is just testing a particular unit, so having a dependency to an implementation instead of an interface really beats the point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top