0
我有以下测试:FakeItEasy - 捕获异常断言消息
[Test]
public void Save_WhenExceptionIsThrown_ThenExceptionIsLogged()
{
A.CallTo(() => this.personRepository.Save(A<PrsPerson>._)).Throws(new Exception("Expected Exception"));
var personen = this.GetPersonenCollectionWithSpecificAmount(2);
this.testee.Save(personen);
A.CallTo(() => this.applicationLogger.Error(A<string>._)).MustHaveHappened(Repeated.Exactly.Once);
}
这个工程就像一个魅力,但我要额外地断言,记录器是否被调用抛出的确切消息是什么异常而不是任何字符串。
是否有可能捕获异常抛出,如果是这样,如何?
在此先感谢
嘿谢谢你的答案。我不想硬编码字符串。我希望能够设置断言:'A.CallTo((=)this.applicationLogger.Error(ex.Message))。MustHaveHappened(Repeated.Exactly.Once);'其中exc是抛出的异常 – xeraphim
Then我认为你最好的办法是 'var ex = new Exception(“Expected Exception”); (()=> this.personRepository.Save(A ._))。抛出(ex); ()=> this.applicationLogger.Error(ex.Message))。MustHaveHappened(Repeated.Exactly.Once);/* * * * * * * * * * * * * * ' 捕获字符串是可能的,但更多的工作,我认为这将工作以及更好地阅读。 –