MOQ - verify exception was thrown

前端 未结 7 1960
暗喜
暗喜 2021-01-07 16:22

I working with MOQ framework for my testing. I have a scenario in which I expect a fault exception to be thrown. How can I verify it was thrown?

public void          


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-07 16:57

    Please read this Introduction to Moq. Here is the way to setup InvalidOperationException throwing when DoSomething method is invoked:

    mock.Setup(foo => foo.DoSomething()).Throws();
    

    Then simply verify if method was called. If it was called, then exception was raised

    mock.Verify(foo => foo.DoSomething());
    

提交回复
热议问题