[英]How do i wireup NServiceBus' SqlSaga in a test scenario?
https://docs.particular.net/nservicebus/testing/有一個非常簡潔的示例,說明了如何測試簡單的Saga。 不幸的是,它沒有解釋如何使用SqlSaga-即。 具有狀態持久性的Saga。
鑒於這個傳奇:
public class SomeFancySaga : SqlSaga<MySagaData>,
IAmStartedByMessages<ImportantCommand>
{
protected override string CorrelationPropertyName => nameof(Data.MyPrimaryKey);
protected override void ConfigureMapping(IMessagePropertyMapper mapper)
{
mapper.ConfigureMapping<ImportantCommand>(x => x.CommandId);
}
public async Task Handle(ImportantCommand command, IMessageHandlerContext context)
{
if (Data.State == MyState.ReadyForUse)
<do some stuff>
}
...
}
如果我嘗試像鏈接中的示例一樣編寫測試代碼,我將執行以下操作:
// arrange
var context = new NServiceBus.Testing.TestableMessageHandlerContext();
var command = ImportantCommand.Create();
var sut = new CreateSomeFancySaga();
// act
sut.Handle(command, context);
// assert
...
調用sut.Handle()會引發NullReferenceException,因為Saga屬性Data尚未初始化 。
如何正確地連接傳奇以進行測試,以便:
我們有一個相關的代碼示例,更詳細地顯示了單元測試: https : //docs.particular.net/samples/unit-testing/ 。 其中包括針對saga的幾個測試( SagaTests.cs
)。
您可以以此為起點,並通過以下方式修改saga測試:
NServiceBus.Persistence.Sql
包。 DiscountPolicy
策略的傳奇,使其繼承自SqlSaga
而不是Saga
。 ConfigureHowToFindSaga
方法)。 希望對您有所幫助,但是如果有任何遺漏或無法通過這種方式測試您的情況,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.