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

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

    Update: Oops.. see that you want this in MSTest. Sorry. Speed read & Misled by your title.

    Try this extension project from Callum Hibbert and see if it works.

    Old response:

    You can do this with NUnit 2.4 and above. See documentation of ExpectedException here

    [ExpectedException( typeof( ArgumentException), ExpectedMessage="unspecified", MatchType=MessageMatch.Contains )]
    public void TestMethod()
    {
    ...
    

    MatchType can be Exact (default), Contains or Regex.. which pretty much handles the 80% use-case. There is also an advanced exception handler method approach if the verification gets too complex.. never used it personally.. didn't need it yet.

提交回复
热议问题