Best way to test exceptions with Assert to ensure they will be thrown

前端 未结 9 1758
悲哀的现实
悲哀的现实 2020-12-02 06:58

Do you think that this is a good way for testing exceptions? Any suggestions?

Exception exception = null;
try{
    //I m sure that an exeption will happen he         


        
9条回答
  •  再見小時候
    2020-12-02 07:57

    Now, 2017, you can do it easier with the new MSTest V2 Framework:

    Assert.ThrowsException(() => myClass.MyMethodWithError());
    
    //async version
    await Assert.ThrowsExceptionAsync(
      () => myObject.SomeMethodAsync()
    );
    

提交回复
热议问题