Can you help me understand Moq Callback?

前端 未结 5 871
傲寒
傲寒 2020-12-09 00:24

Using Moq and looked at Callback but I have not been able to find a simple example to understand how to use it.

Do you have a small working snippet whic

5条回答
  •  感情败类
    2020-12-09 00:57

    On top of the other good answers here, I've used it to perform logic before throwing an exception. For instance, I needed to store all objects that were passed to a method for later verification, and that method (in some test cases) needed to throw an exception. Calling .Throws(...) on Mock.Setup(...) overrides the Callback() action and never calls it. However, by throwing an exception within the Callback, you can still do all of the good stuff that a callback has to offer, and still throw an exception.

提交回复
热议问题