I'm trying to test a method that in a particular scenario should throw an exception which has an inner exception:
mockInvoiceRepository.Setup(x => x.PersistInvoice(invoice, false)).Throws(new Exception());
That line of code is fine except I need to set the innerexception for the new exception that is thrown. I can't do this though because it is a readonly property and also can't mock it because it's not virtual.
Any ideas on how to achieve this?
Would this work? Here I am using the overloaded constructor of Exception that takes a string message (which you don't seem to care about) and an inner exception.
var innerException = new ExpectedInnerException();
mockInvoiceRepository.Setup(x => x.PersistInvoice(invoice, false))
.Throws(new Exception("", innerException));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With