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 ?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top