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");
}
}
}