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

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

    Fluent Assertions (NuGet) has a very "language natural" syntax for defining expectations in unit tests:

    objectundertest.Invoking(o => o.MethodUnderTest()).Should().Throw()
        .WithMessage("the expected error message");
    

    There are multiple variations for checking the error message with any algorithm (Where(e => ...) as well as checking into inner exceptions and their messages.

提交回复
热议问题