Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 342
Stephen Yeager Avatar asked Nov 26 '19 04:11

Stephen Yeager


People also ask

Can a gRPC server also be a client?

Yes, you can definitely do that.

Is gRPC deprecated?

gRPC C-core is in maintenance mode and will be deprecated in favor of gRPC for . NET. gRPC C-core is not recommended for new apps.


1 Answers

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.

like image 103
Francis Avatar answered Oct 16 '22 06:10

Francis