I am playing around with the new GRPC Service for .Net Core 3.0 template. I have successfully created the Hello World service. I am now trying to add a test project to this solution.
I can see how to make an in-memory server for integration testing for a regular HTTP Client/Server .Net Core service. But I can find no example or guidance anywhere on the net for when using grpc protodef client/server. The issue appears to be with the CreateClient() method which returns a plain old HTTP client, and not a grpc one.
It would be good if a barebones integration test was included with the project created by the VS 2019 template. The main reason for me to look at Microservices and in particular using grpc was the idea that I can have small blocks that are easily testable with an automated testing framework. But I seem to have fallen at the first hurdle.
[Added]
Below is the simple test class. It uses NUnit. The issue I have is finding a way to run the in-memory TestHost, the same way as I do for a .Net Core API or MVC app to allow integration testing.
namespace Project.Tests
{
public class Tests
{
private Greeter.GreeterClient _client;
private TestServer _server;
[OneTimeSetUp]
public void OneTimeSetup()
{
var server = new TestServer(WebHost.CreateDefaultBuilder()
.UseStartup<TestStartup>()
.UseEnvironment("Development")
.UseUrls("https://localhost:5001"));
}
[SetUp]
public void Setup()
{
var channel = GrpcChannel.ForAddress("https://localhost:5001");
_client = new Greeter.GreeterClient(channel);
}
[Test]
public async Task Test1()
{
var reply = await _client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
Assert.IsTrue(reply.Message == "Hello GreeterClient");
}
}
}
Integration testing: The gRPC app is hosted in TestServer, an in-memory test server from the Microsoft. AspNetCore. TestHost package. gRPC services are tested by calling them using a gRPC client from a unit testing library.
gRPC is a language agnostic, high-performance Remote Procedure Call (RPC) framework. The main benefits of gRPC are: Modern, high-performance, lightweight RPC framework. Contract-first API development, using Protocol Buffers by default, allowing for language agnostic implementations.
Could you please try to pass an HTTPClient created by test server into GRPC channel?
It works when I using WebApplicationFactory, haven't tested for a test server before.
var channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
{
HttpClient = _server.CreateClient()
});
var client = new Greeter.GreeterClient(channel);
var response await client.SayHelloAsync(new HelloRequest());
// Assert anything you want
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