Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/Tester/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc();
services.AddGrpc(o => o.EnableDetailedErrors = true);
services.AddSingleton<IGreeter, Greeter>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ private void WriteMessage(LogLevel logLevel, string category, EventId eventId, s
// if it is written in the test's execution context.
ExecutionContext.Run(_executionContext, s =>
{
Console.WriteLine($"{_stopwatch.Elapsed.TotalSeconds:N3}s {category} - {logLevel}: {message}");
var log = $"{_stopwatch.Elapsed.TotalSeconds:N3}s {category} - {logLevel}: {message}";
if (exception != null)
{
log += Environment.NewLine + exception.ToString();
}
Console.WriteLine(log);
}, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#endregion

using Grpc.Core;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using NUnit.Framework;
Expand All @@ -30,13 +31,20 @@ protected override void ConfigureServices(IServiceCollection services)
{
var mockGreeter = new Mock<IGreeter>();
mockGreeter.Setup(
m => m.Greet(It.IsAny<string>())).Returns((string s) => $"Test {s}");
m => m.Greet(It.IsAny<string>())).Returns((string s) =>
{
if (string.IsNullOrEmpty(s))
{
throw new ArgumentException("Name not provided.");
}
return $"Test {s}";
});

services.AddSingleton(mockGreeter.Object);
}

[Test]
public async Task SayHelloUnaryTest_MockGreeter()
public async Task SayHelloUnaryTest_MockGreeter_Success()
{
// Arrange
var client = new Tester.TesterClient(Channel);
Expand All @@ -48,5 +56,24 @@ public async Task SayHelloUnaryTest_MockGreeter()
// Assert
Assert.AreEqual("Test Joe", response.Message);
}

[Test]
public async Task SayHelloUnaryTest_MockGreeter_Error()
{
// Arrange
var client = new Tester.TesterClient(Channel);

// Act
try
{
await client.SayHelloUnaryAsync(new HelloRequest { Name = "" });
Assert.Fail();
}
catch (RpcException ex)
{
// Assert
StringAssert.Contains("Name not provided.", ex.Status.Detail);
}
}
}
}