How can I test for an expected exception with a specific exception message from a resource file in Visual Studio Test?

前端 未结 7 1936
广开言路
广开言路 2020-12-13 09:11

Visual Studio Test can check for expected exceptions using the ExpectedException attribute. You can pass in an exception like this:

[TestMethod]
[ExpectedExc         


        
相关标签:
7条回答
  • 2020-12-13 09:49

    I would recommend using a helper method instead of an attribute. Something like this:

    public static class ExceptionAssert
    {
      public static T Throws<T>(Action action) where T : Exception
      {
        try
        {
          action();
        }
        catch (T ex)
        {
          return ex;
        }
        Assert.Fail("Exception of type {0} should be thrown.", typeof(T));
    
        //  The compiler doesn't know that Assert.Fail
        //  will always throw an exception
        return null;
      }
    }
    

    Then you can write your test something like this:

    [TestMethod]
    public void GetOrganisation_MultipleOrganisations_ThrowsException()
    {
      OrganizationList organizations = new Organizations();
      organizations.Add(new Organization());
      organizations.Add(new Organization());
    
      var ex = ExceptionAssert.Throws<CriticalException>(
                  () => organizations.GetOrganization());
      Assert.AreEqual(MyRes.MultipleOrganisationsNotAllowed, ex.Message);
    }
    

    This also has the benefit that it verifies that the exception is thrown on the line you were expecting it to be thrown instead of anywhere in your test method.

    0 讨论(0)
提交回复
热议问题