Assert exception from NUnit to MS TEST

后端 未结 3 1190
终归单人心
终归单人心 2021-01-04 05:10

I have some tests where i am checking for parameter name in exception. How do i write this in MS TEST?

ArgumentNullExc         


        
3条回答
  •  盖世英雄少女心
    2021-01-04 05:56

    public static class ExceptionAssert
    {
      public static T Throws(Action action) where T : Exception
      {
        try
        {
          action();
        }
        catch (T ex)
        {
          return ex;
        }
    
        Assert.Fail("Expected exception of type {0}.", typeof(T));
    
        return null;
      }
    }
    

    You can use the extension method above as a test helper. Here is an example of how to use it:

    // test method
    var exception = ExceptionAssert.Throws(
                  () => organizations.GetOrganization());
    Assert.AreEqual("lawbaseFixedContactRepository", exception.ParamName);
    

提交回复
热议问题