我在滷麪這個簡單之城:單元測試問題
public void MySaga : Saga<MySagaData>
IAmInitiatedBy<Event1>
IHandleMessages<Event2>
{
private IBus bus;
private ILog logger;
public MySaga(IBus bus, ILog logger)
{
if (bus == null) throw new ArgumentNullException("bus");
if (logger == null) throw new ArgumentNullException("logger");
this.bus = bus;
this.logger = logger;
}
protected override void CorrelateMessages(ICorrelationConfig<MySagaData> config)
{
config.Correlate<Event>(m => m.MyObjectId.Id, s => s.Id);
config.Correlate<Event>(m => m.MyObjectId.Id, s => s.Id);
}
public Task Handle(Event1 message)
{
return Task.Run(() =>
{
this.Data.Id = message.MyObjectId.Id;
this.Data.State = MyEnumSagaData.Step1;
var cmd = new ResponseCommandToEvent1(message.MyObjectId);
bus.Send(cmd);
});
}
public Task Handle(Event2 message)
{
return Task.Run(() =>
{
this.Data.State = MyEnumSagaData.Step2;
var cmd = new ResponseCommandToEvent2(message.MyObjectId);
bus.Send(cmd);
});
}
}
多虧了那種mookid8000我可以用FakeBus和SagaFixture測試傳奇:
[TestInitialize]
public void TestInitialize()
{
var log = new Mock<ILog>();
bus = new FakeBus();
fixture = SagaFixture.For<MySaga>(() => new MySaga(bus, log.Object));
idTest = new MyObjectId(Guid.Parse("1B2E7286-97E5-4978-B5B0-D288D71AD670"));
}
[TestMethod]
public void TestIAmInitiatedBy()
{
evt = new Event1(idTest);
fixture.Deliver(evt);
var testableFixture = fixture.Data.OfType<MySagaData>().First();
Assert.AreEqual(MyEnumSagaData.Step1, testableFixture.State);
// ... more asserts
}
[TestMethod]
public void TestIHandleMessages()
{
evt = new Event2(idTest);
fixture.Deliver(evt);
var testableFixture = fixture.Data.OfType<MySagaData>().First();
Assert.AreEqual(MyEnumSagaData.Step2, testableFixture.State);
// ... more asserts
}
[TestCleanup]
public void TestCleanup()
{
fixture.Dispose();
bus.Dispose();
}
檢查IAmInitiatedBy
的第一個測試方法已正確執行且未引發錯誤,而第二個測試失敗。它看起來像是一個相關性問題,因爲fixture.Data
不包含任何元素,並且在fixture.LogEvents
中包含最後一個元素:找不到消息Event2/b91d161b-eb1b-419d-9576-2c13cd9d9c51的現有傳奇數據。
這是什麼GUID?與我在單元測試中定義的完全不同嗎?有任何想法嗎?我嘗試測試是否合法(因爲我使用的是內存總線)?
我沒有分配佐賀ID,因爲這發生在相關階段,這就是爲什麼即使刪除壞行的傳奇ID仍然等於'message.MyObjectId.Id'。我對嗎?對於第二點,我用'[ClassInitialize]'替換了'[TestInitialize]',它現在可以工作。再次感謝你... –