简体   繁体   English

MOQ设置函数抛出异常不起作用

[英]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. 希望它足够了,有些人可以帮助我。

Looks like the problem is in the params parameter. 看起来问题出在params参数中。 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM