AnonyMouse
AnonyMouse

Reputation: 18650

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?

Upvotes: 3

Views: 2012

Answers (1)

Austin Salonen
Austin Salonen

Reputation: 50235

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));

Upvotes: 7

Related Questions