I've been trying to look for examples on the internet or anywhere of how to implement gRPC unit testing in c# but can't for the love of me find anything, or I could just be over seeing things.
If someone could point me in the right direction I would be very grateful.
S
I usually do something like this
1: I create an interface for the service to test the DI system and or to be able to mock it. I do this as registering the gRPC service with the framework kinde-does-that as well as it's created similar to a controller
Then I write my Test like so:
[TestMethod]
public async Task Call_Grpc_Method_Test()
{
    var grpcService = _service.GetRequiredService<IMyService>();
    var request = new MyRequest {When=DateTime.Now.AddDays(30)};
    
    var context = TestServerCallContext.Create(
                                             method: nameof(IMyService.WinningLotteryNumbers)
                                            , host: "localhost"
                                            , deadline: DateTime.Now.AddMinutes(30)
                                            , requestHeaders: new Metadata()
                                            , cancellationToken: CancellationToken.None
                                            , peer: "10.0.0.25:5001"
                                            , authContext: null
                                            , contextPropagationToken: null
                                            , writeHeadersFunc: (metadata) => Task.CompletedTask
                                            , writeOptionsGetter: () => new WriteOptions()
                                            , writeOptionsSetter: (writeOptions) => { }
                                            ) ;
    var answer= await grpcService.WinningLotteryNumbers(request, context);
    Assert.IsNotNull(answer);
}
IMyService is the gRPC service I want to test and I just create an IServiceProvider that can build one for me via the actual implementations making it an integration test or via mocked objects making it a unit test.
The TestServerCallContext class is located in the NuGet package Grpc.Core.Testing, you would need to add that to your test project.
as to the constructor, well you can use hard coded sample data as I demonstrated or go fancy and add meta data for headers and authcontex as well as the stuff when you need it...
As you can see, not that hard to do once you get started ;-)
For simple unary services, as long as you're not doing anything interesting with gRPC headers, cancellation, etc - you should be able to treat your server type just as the service type - create an instance, call the simple unary method(s), check the results. However, this won't validate marshalling or the gRPC layer, etc. For that, you really need an integration test. Likewise, anything involving streaming probably needs an integration test, because there's a lot of background plumbing. If this was me, I would simply create a server as a test-fixture, and write my tests by creating a genuine client that can talk to those services.
If you're using the Google server code, something like the fixture I'm using here should do (don't worry about the AddCodeFirst - that's some protobuf-net.Grpc additions; just use the same registration code that you would have used in the real server). If you need to test with the Microsoft server code, you'd need to host Kestrel in process as the fixture; but fortunately: they're mostly completely interchangeable, so if in doubt: use whichever is simpler. Note that on the client side: again, since the idea of gRPC is to be transparently interoperable between languages/runtimes/frameworks, it shouldn't matter whether the client uses the Microsoft or Google transport. Historically there have been some minor differences, but these are a: pretty niche, and b: get fixed when found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With