Mbunit Factory attribute with ExpectedException

Is there a way to write that I'm expecting a certain exception for certain inputs when I use the Factory attribute? I know how to do it using the Row attribute but I need it for dynamically generated test inputs.

See test example bellow for a function that returns the inverse of the provided string:

[TestFixture]
public class MyTestFixture()
{
   private IEnumerable<object[]> TestData
   {
      get
      {
          yield return new object[] { "MyWord", "droWyM" };
          yield return new object[] { null, null }; // Expected argument exception
          yield return new object[] { "", "" };
          yield return new object[] { "123", "321" };
      }
   }

   [Test, Factory("TestData")]
   public void MyTestMethod(string input, string expectedResult)
   {
      // Test logic here...   
   }
}

I'm afraid that there is no built-in functionality to attach metadata (such as an expected exception) to a row of test parameters coming from a factory method.

However, a simple solution is to pass the type of the expected exception as a test regular parameter (null if no exception is expected to be thrown) and to enclose the tested code in an Assert.Throws or Assert.DoesNotThrow method.

[TestFixture]
public class MyTestFixture()
{
  private IEnumerable<object[]> TestData
  {
    get
    {
        yield return new object[] { "MyWord", "droWyM", null };
        yield return new object[] { null, null, typeof(ArgumentNullException) };
        yield return new object[] { "", "", null };
        yield return new object[] { "123", "321", null };
    }
  }

  [Test, Factory("TestData")]
  public void MyTestMethod(string input, string expectedResult, Type expectedException)
  {
    RunWithPossibleExpectedException(expectedException, () => 
    {
       // Test logic here... 
    });
  }

  private void RunWithPossibleExpectedException(Type expectedException, Action action)
  {
    if (expectedException == null)
      Assert.DoesNotThrow(action);
    else
      Assert.Throws(expectedException, action);
  }
}

By the way, it could be interesting to have an extra Assert.MayThrow assertion to get rid of the helper method. It could just accept null as the expected exception type. Maybe you could create a feature request here, or you may submit a patch.

链接地址: http://www.djcxy.com/p/73380.html

上一篇: 检查,如果所有的枚举值被处理

下一篇: 带有ExpectedException的Mbunit Factory属性