Stephen Yeager
Stephen Yeager

Reputation: 123

Create interfaces for gRPC client for unit tests

I have a dotnet core 3 console app project with a generated gRPC client (using the Protobuf element in the csproj below). I would like to unit test my code. Is there a way to generate my gRPC client to include interfaces for the generated classes so that I can properly mock out the gRPC client?

Thank you for your time!

<ItemGroup>
    <Protobuf Include="..\..\Data\Protos\*" GrpcServices="Client" />
</ItemGroup>

Upvotes: 8

Views: 4878

Answers (1)

Francis
Francis

Reputation: 1904

The folks at Google decided not to include Interfaces going forward (They used to generate it one point).

The primary reason they've cited is that Interfaces can't maintain backward/forward compatibility that the underlying protobuf requires. If you change an interface, this will break the build and any compatibility with previous builds.

You can read more about it here.

As for testing the generated abstract classes, you can use a Mocking Framework such as Moq to test it out but sounds like you're already aware of that most likely. If not, there's an example here.

Upvotes: 4

Related Questions