Skip to content

Commit 3934c6c

Browse files
authored
Improve tester example to log errors and have non-success test (#1841)
1 parent a3717a3 commit 3934c6c

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

examples/Tester/Server/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Startup
2727
{
2828
public void ConfigureServices(IServiceCollection services)
2929
{
30-
services.AddGrpc();
30+
services.AddGrpc(o => o.EnableDetailedErrors = true);
3131
services.AddSingleton<IGreeter, Greeter>();
3232
}
3333

examples/Tester/Tests/Server/IntegrationTests/Helpers/GrpcTestContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ private void WriteMessage(LogLevel logLevel, string category, EventId eventId, s
4444
// if it is written in the test's execution context.
4545
ExecutionContext.Run(_executionContext, s =>
4646
{
47-
Console.WriteLine($"{_stopwatch.Elapsed.TotalSeconds:N3}s {category} - {logLevel}: {message}");
47+
var log = $"{_stopwatch.Elapsed.TotalSeconds:N3}s {category} - {logLevel}: {message}";
48+
if (exception != null)
49+
{
50+
log += Environment.NewLine + exception.ToString();
51+
}
52+
Console.WriteLine(log);
4853
}, null);
4954
}
5055

examples/Tester/Tests/Server/IntegrationTests/MockedGreeterServiceTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#endregion
1818

19+
using Grpc.Core;
1920
using Microsoft.Extensions.DependencyInjection;
2021
using Moq;
2122
using NUnit.Framework;
@@ -30,13 +31,20 @@ protected override void ConfigureServices(IServiceCollection services)
3031
{
3132
var mockGreeter = new Mock<IGreeter>();
3233
mockGreeter.Setup(
33-
m => m.Greet(It.IsAny<string>())).Returns((string s) => $"Test {s}");
34+
m => m.Greet(It.IsAny<string>())).Returns((string s) =>
35+
{
36+
if (string.IsNullOrEmpty(s))
37+
{
38+
throw new ArgumentException("Name not provided.");
39+
}
40+
return $"Test {s}";
41+
});
3442

3543
services.AddSingleton(mockGreeter.Object);
3644
}
3745

3846
[Test]
39-
public async Task SayHelloUnaryTest_MockGreeter()
47+
public async Task SayHelloUnaryTest_MockGreeter_Success()
4048
{
4149
// Arrange
4250
var client = new Tester.TesterClient(Channel);
@@ -48,5 +56,24 @@ public async Task SayHelloUnaryTest_MockGreeter()
4856
// Assert
4957
Assert.AreEqual("Test Joe", response.Message);
5058
}
59+
60+
[Test]
61+
public async Task SayHelloUnaryTest_MockGreeter_Error()
62+
{
63+
// Arrange
64+
var client = new Tester.TesterClient(Channel);
65+
66+
// Act
67+
try
68+
{
69+
await client.SayHelloUnaryAsync(new HelloRequest { Name = "" });
70+
Assert.Fail();
71+
}
72+
catch (RpcException ex)
73+
{
74+
// Assert
75+
StringAssert.Contains("Name not provided.", ex.Status.Detail);
76+
}
77+
}
5178
}
5279
}

0 commit comments

Comments
 (0)