In MSTest, How can I verify exact error message using [ExpectedException(typeof(ApplicationException))]

前端 未结 11 1522
南方客
南方客 2020-12-15 16:01

Using MSTest how can I verify the exact error message coming from a test method? I know [ExpectedException(typeof(ApplicationException), error msg)] doesn\'t co

11条回答
  •  死守一世寂寞
    2020-12-15 16:44

    MSTest v2 supports Assert.Throws and Assert.ThrowsAsync which returns the captured exception.

    Here is an article on how to upgrade to MSTest v2: https://blogs.msdn.microsoft.com/devops/2017/09/01/upgrade-to-mstest-v2/

    Here is example usage:

    var myObject = new MyObject();
    var ex = Assert.Throws(() => myObject.Do(null));
    StringAssert.Contains(ex.Message, "Parameter name: myArg");
    

提交回复
热议问题