gpt4 book ai didi

c# - 我应该将私有(private)方法提取到单元测试类吗?

转载 作者:行者123 更新时间:2023-11-28 21:18:39 24 4
gpt4 key购买 nike

<分区>

我有以下类,其中我想要进行单元测试的所有逻辑都发生在我提取的私有(private)方法中,以供两个要调用的 Handle 方法重用。我坚持的是对这个类进行单元测试的正确方法。我想断言基本上有 4 种可观察到的行为:

  1. 当注册不存在时不调用“替换”
  2. 当文档不存在时不调用“替换”
  3. 当文档消息是最新的时不调用“替换”
  4. 当所有其他条件都满足时调用“替换”

我可以轻松地复制两个公共(public) Handle 方法的所有单元测试。但我想知道将私有(private)方法提取到使用我可以直接测试的公共(public)方法公开此行为的类是否更合适。

想法?

public class Denormalizer :
INotificationHandler<LightsMessageReceived>,
INotificationHandler<AnnouncementsMessageReceived>
{
public Task Handle(AnnouncementsMessageReceived notification, CancellationToken cancellationToken)
{
return HandleMessage(notification, document => document.Announcements, cancellationToken);
}

public Task Handle(LightsMessageReceived notification, CancellationToken cancellationToken)
{
return HandleMessage(notification, document => document.Lights, cancellationToken);
}

private async Task HandleMessage(InventoryMessage message, Func<SaleRegistration, ISequenced> getMessageId, CancellationToken cancellationToken)
{
var registration = await docContext.CsoMessage.GetSaleRegistration(message.SiteId, message.InventoryId, cancellationToken);

if (registration == null) return;

var document = await docContext.SaleRegistration.Get(message.SiteId, message.InventoryId,
registration.Data.SaleDate, registration.Data.SaleType, cancellationToken);

if (document == null) return;

if (getMessageId(document).IsCurrent(message.MessageId)) return;

document.Map(message);

await docContext.SaleRegistration.Replace(document);
}
}

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