如何最小起订量委托回调和 lambda 方法
Posted
技术标签:
【中文标题】如何最小起订量委托回调和 lambda 方法【英文标题】:How to moq delegate CallBack and lambda method 【发布时间】:2022-01-13 13:59:16 【问题描述】:我在 C# SendAnprProviderExemptionNotifications
中定义了委托,它在方法 ProcessCreateRequestAsync
中作为参数传递,触发回调方法 SendMessagesAsync
。我正在努力模拟这个采用 ProviderExemptionReceivedNotification
对象的回调函数。
起订量尝试
ringGoApiServiceMoq.Setup(x => x.ProcessCreateRequestAsync(It.IsAny<RingGoExemption>(), It.IsAny<SendAnprProviderExemptionNotifications>()))
.ReturnsAsync(ringGoMessageResponseResultMoq)
.Callback<ProviderExemptionReceivedNotification>(providerExemptionReceivedNotificationMoq);
委托定义
public delegate Task<bool> SendAnprProviderExemptionNotifications(IEnumerable<ProviderExemptionReceivedNotification> exemptions);
public interface IRingGoApiService
Task<RingGoMessageResponseResult> ProcessCreateRequestAsync(RingGoExemption ringGoExemption, SendAnprProviderExemptionNotifications sendProviderExemption);
以下代码实现了委托;
实施
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "POST")] HttpRequest req
, [ServiceBus("MyServiceBus", Connection = "MyServiceBusConnection")] IAsyncCollector<Message> servicebusMessage
)
dynamic responseMessage = null;
try
string ringGoTransaction = await new StreamReader(req.Body).ReadToEndAsync();
if (!string.IsNullOrEmpty(ringGoTransaction))
var ringGoExemption = MapRingGoExemption(ringGoTransaction);
var result = await _ringGoApiService.ProcessCreateRequestAsync(ringGoExemption,
async (IEnumerable<ProviderExemptionReceivedNotification> exemptions) => await SendMessagesAsync(servicebusMessage, exemptions)
); //This is where I calling Delegate ..
if (result != null)
//Remaining Code...
回调
static async Task<bool> SendMessagesAsync(IAsyncCollector<Message> collector, IEnumerable<ProviderExemptionReceivedNotification> exemptions)
SetProviderExemption(exemptions);
xUnit 测试
[Fact]
public void MyTestA()
//Arrange
var fixture = new Fixture();
var ringGoTransaction = RingGoExemptionTestData.GetRingGoSession();
Mock<HttpRequest> mockHttpRequest = httpResquestFactory.CreateMockHttpRequest(ringGoTransaction);
fixture.Customize<ProviderLocation>(c => c
.With(x => x.ProviderId, 13)
.With(x => x.CreatedBy, 1)
.With(x => x.LocationReference, "222")
.With(x => x.SiteId, 215)
.With(x => x.IsActive, true)
);
var providerLocationDataMoq = fixture.Create<ProviderLocation>();
providerExemptionServiceMoq.Setup(x => x.GetProviderLocation(13, "222")).ReturnsAsync(providerLocationDataMoq);
var providerExemptionReceivedNotificationMoq = fixture.Create<ProviderExemptionReceivedNotification>();
fixture.Customize<RingGoMessageResponseResult>(a => a
.With(x => x.ProviderSessionId, "1023446448745110767")
.With(x => x.IsSuccess, true)
);
var ringGoMessageResponseResultMoq = fixture.Create<RingGoMessageResponseResult>();
ringGoApiServiceMoq.Setup(x => x.ProcessCreateRequestAsync(It.IsAny<RingGoExemption>(), It.IsAny<SendAnprProviderExemptionNotifications>()))
.ReturnsAsync(ringGoMessageResponseResultMoq)
.Callback<ProviderExemptionReceivedNotification>(providerExemptionReceivedNotificationMoq); // How to add CallBack moq which takes ProviderExemptionReceivedNotification object
【问题讨论】:
您是否可以尝试缩小确切的问题范围?我敢肯定那里有一些很好的答案,但这个问题有点像“文字墙”:) 【参考方案1】: var providerExemptionReceivedNotificationMoq = RingGoExemptionTestData.GetProviderExemptionReceivedNotification();
ringGoApiServiceMoq.Setup(x => x.ProcessCreateRequestAsync(It.IsAny<RingGoExemption>(), It.IsAny<SendAnprProviderExemptionNotifications>()))
.ReturnsAsync(ringGoMessageResponseResultMoq)
.Callback((RingGoExemption exemption, SendAnprProviderExemptionNotifications sendProviderExemption) =>
sendProviderExemption.Invoke(providerExemptionReceivedNotificationMoq);
);
【讨论】:
以上是关于如何最小起订量委托回调和 lambda 方法的主要内容,如果未能解决你的问题,请参考以下文章