Dennis
Dennis

Reputation: 1848

MOQ: Throwing exception that was passed into a method

I have a method on an interface:

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

I want to mock this with MOQ, and on any parameters 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] )

Upvotes: 4

Views: 1089

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You can use a callback action which is invoked with parameters passed to 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; });

Upvotes: 6

Related Questions