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

前端 未结 11 1500
南方客
南方客 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:53

    In MSTest there's no built-in way of doing it. This is about as 'elegant' as it gets:

    [TestMethod]
    public void Test8()
    {
        var t = new Thrower();
        try
        {
            t.DoStuffThatThrows();
            Assert.Fail("Exception expected.");
        }
        catch (InvalidOperationException e)
        {
            Assert.AreEqual("Boo hiss!", e.Message);
        }
    }
    

    However, you could consider porting xUnit.NET's Assert.Throws API to a custom library - that's what we did.

    You could also go to Microsoft Connect an vote on this suggestion.

提交回复
热议问题