我正在尝试为我的传奇编写单元测试。我使用 SagaFixture 来托管我的 Saga 并使用 FakeBus。我还使用了 FakeMessageContext,因为在 Saga 执行中我使用 MessageContext 来获取返回地址。
现在,当我试图在 Fixture 上调用 Handle 方法时,它会抛出 NullReferenceException 类型的异常。
粘贴代码如下:
搜索佐贺
public class SearchSaga : Saga<SearchSagaData>, IAmInitiatedBy<PersonRequested>, IHandleMessages<PersonSearchCompleted>
{
public void Handle(PersonRequested message)
{
Data.Id = new Guid(message.MessageId);
Data.ReturnAddress = MessageContext.GetCurrent().ReturnAddress;
Bus.Publish(message);
}
}
SagaUnitTest
[Test]
public void PublishResponseOfTypeSearchPersonRequest()
{
var bus = new FakeBus();
var saga = new SearchSaga(bus);
var fixture = new SagaFixture<SearchSagaData>(saga);
FakeMessageContext.Reset();
var fakeContext = MockRepository.GenerateMock<IMessageContext>();
fakeContext.Stub(s => s.ReturnAddress).Return("queuename");
fakeContext.Stub(s => s.Headers).Return(new Dictionary<string, object>());
// act
using (FakeMessageContext.Establish(fakeContext))
{
fixture.Handle(new PersonRequested {MessageId = Guid.NewGuid().ToString(), Query = "Abc"});
}
var sentRequests = bus.PublishedMessages.OfType<SearchPersonRequest>().ToList();
Assert.That(sentRequests.Count, Is.EqualTo(1));
}
错误堆栈跟踪:在 Rebus.SagaContext..ctor(Guid id) 在 Rebus.Bus.Dispatcher.DispatchToHandler[TMessage](TMessage 消息,IHandleMessages 1 handler) at Rebus.Testing.SagaFixture
1.Handle[TMessage](TMessage 消息)