Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MOQ setup function to throw exception doesn't work

I have the following function signature:

T SomeMethod(Expression<Func<T, string>> param1, 
             , params Expression<Func<T, object>>[] items);

I want it to throw an exception everytime it's executed.

I tried to do the following setup:

myMock.Setup(x => x.SomeMethod(Moq.It.IsAny<Expression<Func<SomeClass, string>>>()))
      .Throws(new Exception());

Everything works find but when I arrive to this method it does't throw an exception (although the object is my mock).

I assume my setup is incorrect.

I tried many variation for a while and now I'm a bit frustrated.

I would have put more code but it is restricted. Each piece I want to upload I should alter so excuse me for being cheap with information.

Hope it's enough and some one can assist me.

like image 534
Gal Ziv Avatar asked Apr 24 '13 13:04

Gal Ziv


1 Answers

Looks like the problem is in the params parameter. Try adding it to the setup

myMock.Setup(x => x.SomeMethod(
         Moq.It.IsAny<Expression<Func<SomeClass, string>>>()),
         Moq.It.IsAny<Expression<Func<T, object>>[]>())
      ).Throws(new Exception());
like image 134
alex Avatar answered Oct 12 '22 16:10

alex