gpt4 book ai didi

c# - 用于单元测试的模拟 gRPC 响应流 - C#

转载 作者:行者123 更新时间:2023-12-01 23:33:34 27 4
gpt4 key购买 nike

我有一个调用 gRPC 端点的函数,将对象转换为 POCO 对象并将它们作为列表返回。

public class ActionPlanConnectionsService : IConnectionService
{
#region Fields

/// <summary>
/// Grpc client
/// </summary>
private readonly ConnectionDb.ConnectionDbClient _client;
#endregion

public ActionPlanConnectionsService(ConnectionDb.ConnectionDbClient channel)
{
_client = channel;
}

public async Task<IEnumerable<Connection>> Get(int actionPlanId, int implementation)
{
List<Connection> diagramConnections = new List<Connection>();
GetConnectionsByIdAndImplementationMessage message = new GetConnectionsByIdAndImplementationMessage
{
ActionPlanId = actionPlanId,
Implementation = implementation
};

using var call = _client.GetAllConnections(message);
await foreach (ConnectionServiceModel connection in call.ResponseStream.ReadAllAsync())
{
// Never enters here as ResponseStream has no elements when unit testing!!
diagramConnections.Add(new Connection(
connection.FromActionPlanStepId, connection.ToActionPlanStepId, connection.ActionPlanId,
connection.Qualifier, connection.Implementation, connection.Path));
}

return diagramConnections;
}
}

我一直在为此函数开发单元测试,但返回的列表始终具有零计数。这是因为 ResponseStream 中没有元素。

如何模拟 ResponseStream?

到目前为止我的单元测试:

[Test]
public async Task GetConnectionsTest()
{
// Arrange
Mock<ConnectionDb.ConnectionDbClient> mockClient = new Mock<ConnectionDb.ConnectionDbClient>();
Mock<IAsyncStreamReader<ConnectionServiceModel>> mockResponseStream
= new Mock<IAsyncStreamReader<ConnectionServiceModel>>();

List<ConnectionServiceModel> connectionServiceModels =
new List<ConnectionServiceModel>
{
new ConnectionServiceModel
{
ActionPlanId = 1,
FromActionPlanStepId = 1,
ToActionPlanStepId = 1,
Implementation = 0,
Qualifier = 1,
Path = " 1;2;3;4;5;6;7;8;9;10;11;12;13;14"
}
};

var fakeCall = TestCalls.AsyncServerStreamingCall
(mockResponseStream.Object,
Task.FromResult(new Metadata()), () => Status.DefaultSuccess,
() => new Metadata(), () => { });

mockClient.Setup(m => m.GetAllConnections(
It.IsAny<GetConnectionsByIdAndImplementationMessage>(),
null, null, CancellationToken.None)).Returns(fakeCall);

// Act
ActionPlanConnectionsService service = new ActionPlanConnectionsService(mockClient.Object);
IEnumerable<Connection> connections = await service.Get(1, 1);

// Assert

// CONNECTIONS WILL ALWAYS HAVE 0 Elements as the response isn't setup for it.
}
}

最佳答案

最简单的方法是模拟 IAsyncStreamReader 枚举器并将其提供给 AsyncServerStreamingCall:

var asyncStreamReader = new Mock<IAsyncStreamReader<MyResponse>>();
var asyncServerStreamingCall = new AsyncServerStreamingCall<MyResponse>
(asyncStreamReader.Object, null, null, null, null, null);

var l = new List<MyResponse>
{
new MyResponse{},
new MyResponse(),
new MyResponse(),
};

var enumerator = l.GetEnumerator();

asyncStreamReader.Setup(x => x.MoveNext(It.IsAny<CancellationToken>
())).ReturnsAsync(() => enumerator.MoveNext());
asyncStreamReader.Setup(x => x.Current).Returns(() => enumerator.Current);

myGrpcService.Setup(s => s.CallService(It.IsAny<MyRequest>(), null, null,It.IsAny<CancellationToken>())).Returns(asyncServerStreamingCall);

关于c# - 用于单元测试的模拟 gRPC 响应流 - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65917871/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com