gpt4 book ai didi

c# - RhinoMock - AssertWasCalled 在具有不同参数的相同方法上不起作用

转载 作者:行者123 更新时间:2023-11-28 20:49:06 27 4
gpt4 key购买 nike

我有这个方法:

        public void Handle(ShipmentConfirmedEvent message)
{
try
{
var trackingOrderDto = new ShipmentConfirmedDto
{
AccountId = message.AccountId,
FirstName = message.CustomerFirstName,
TransactionId = message.TransactionId,
Language = message.LanguageCode,
ExcludedCommunicationChannels = message.ExcludedCommunicationChannels,
TrackingId = message.TrackingNumber
};

Log.Info("Received notify of Shipment Confirmed request with id <{0}> for order {1}", message.GenerationId, message.TransactionId);
if (!string.IsNullOrEmpty(_iosForcePushAccountId))
{
Log.Info("Per device Ios, l'accountId {0} verrà forzato a {1} per configurazione", trackingOrderDto.AccountId, _iosForcePushAccountId);
trackingOrderDto.AccountId = Convert.ToInt64(_iosForcePushAccountId);
}
Log.Debug("Send push notification for IOS for AccountId <{0}>", trackingOrderDto.AccountId);
var iosContent = _sender.Create(trackingOrderDto, "IOS");
_sender.IosSend(iosContent);

trackingOrderDto.AccountId = message.AccountId;

if (!string.IsNullOrEmpty(_androidForcePushAccountId))
{
Log.Info("Per device Android, l'accountId {0} verrà forzato a {1} per configurazione", trackingOrderDto.AccountId, _androidForcePushAccountId);
trackingOrderDto.AccountId = Convert.ToInt64(_androidForcePushAccountId);
}
Log.Debug("Send push notification for Android for AccountId <{0}>", trackingOrderDto.AccountId);
var androidContent = _sender.Create(trackingOrderDto, "ANDROID");
_sender.AndroidSend(androidContent);
}
catch (ExcludedCommunicationChannelsException ex)
{
Log.Warn(ex.Message);
}
catch (Exception ex)
{
var msg = String.Format("Errore durante la gestione del messaggio relativo al PushNotification for username {0}", message.MailTo.First().Address);
Log.Error(ex, msg);
throw new Exception(ex.Message, ex);
}
Log.Info("<{0}> Processed", message.GenerationId);
}

我有这个测试:

[Fact]
public void GivenAnOrderConfirmedEventWithForcedAccountId_WhenHandle_ThenCallSenderWithTheForcedAccountId()
{
NServiceBus.Testing.Test.Initialize();

var service = new ShipmentConfirmedRequestEventConsumer(mockSender, "1111", "2222");

NServiceBus.Testing.Test.Handler(svc => service)
.OnMessage<ShipmentConfirmedEvent>(
m =>
{
m.AccountId = 407716;
m.CustomerFirstName = "Marco";
m.TransactionId = "T123456";
m.LanguageCode = "IT";
m.TrackingNumber = "642167921";
}
);
mockSender.AssertWasCalled(x => x.Create(Arg<ShipmentConfirmedDto>.Matches(dto => dto.AccountId == 2222), Arg<string>.Is.Equal("ANDROID")));
mockSender.AssertWasCalled(x => x.Create(Arg<ShipmentConfirmedDto>.Matches(dto => dto.AccountId == 1111), Arg<string>.Is.Equal("IOS")));
}

第二个AssertWasCalled,IOS相关的,抛出异常,因为好像被覆盖了,在第一个被调用的方法内。

我需要验证 Create 方法在方法中被调用了两次,并且指定了参数。

如何修改测试以验证它?

最佳答案

解决方法是这样修改test:

[Fact]
public void GivenAnOrderConfirmedEventWithForcedAccountId_WhenHandle_ThenCallSenderWithTheForcedAccountId()
{
NServiceBus.Testing.Test.Initialize();
mockSender.Expect(x => x.Create(Arg<ShipmentConfirmedDto>.Matches(dto => dto.AccountId == 2222), Arg<string>.Is.Equal("ANDROID"))).Return(string.Empty);
mockSender.Expect(x => x.Create(Arg<ShipmentConfirmedDto>.Matches(dto => dto.AccountId == 1111), Arg<string>.Is.Equal("IOS"))).Return(string.Empty);
var service = new ShipmentConfirmedRequestEventConsumer(mockSender, "1111", "2222");

NServiceBus.Testing.Test.Handler(svc => service)
.OnMessage<ShipmentConfirmedEvent>(
m =>
{
m.AccountId = 407716;
m.CustomerFirstName = "Marco";
m.TransactionId = "T123456";
m.LanguageCode = "IT";
m.TrackingNumber = "642167921";
}
);

mockSender.VerifyAllExpectations();
}

关于c# - RhinoMock - AssertWasCalled 在具有不同参数的相同方法上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54294013/

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