MOQ - verify exception was thrown

前端 未结 7 1868
暗喜
暗喜 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:59

    Reading through these answers I realized there is yet another way to do this using NUnit. The following gets the exception text from an exception and verifies the error message text.

    var ex = Assert.Throws(() => foo.Bar());
    Assert.That(ex.Message, Is.EqualTo("Expected exception text");
    

    And I couldn't get the decoration / attribute syntax to work (AlanT's answer above) using the latest version of NUnit -- not sure why, but it complained no matter what I tried to do.

提交回复
热议问题