MOQ: Throwing the exception that was passed to the method

I have a method on the interface:

void HandleError(MyClasss c, object o, Exception e);

      

I want to mock this with MOQ and any options throwing the supplied Exception e.

Something like:

_mock.Setup(a => a.HandlreError(It.IsAny<MyClass>(), It.IsAny<object>()
   , It.IsAny<Exception>())).Throws( [the 'any' exception] )

      

+3


source to share


1 answer


You can use a callback action that is called with parameters passed to the mocked object:



_mock
.Setup(a => a.HandlreError(It.IsAny<MyClass>(), It.IsAny<object>(), It.IsAny<Exception>()))
.Callback((MyClass c, object o, Exception e) => { throw e; });

      

+4


source







All Articles