Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing set innerexception

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?

like image 475
AnonyMouse Avatar asked Oct 21 '25 05:10

AnonyMouse


1 Answers

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));
like image 70
Austin Salonen Avatar answered Oct 23 '25 21:10

Austin Salonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!