Как мне объявить метод, используя Func <T, TResult> в качестве входного аргумента? - PullRequest
1 голос
/ 16 февраля 2009

Hy,

Я пишу класс, который должен помочь мне в модульных тестах. Класс предлагает методы для выполнения утверждений об исключениях.

До сих пор я мог писать методы, которые принимают функцию без параметров и без возвращаемого значения в качестве входных данных. Для этого я использую System.Action - делегаты. Мой класс выглядит так:

internal static class ExceptionAssert
{
    /// <summary>
    /// Checks to make sure that the input delegate throws a exception of type TException.
    /// <para>
    /// The input delegate must be a method with no parameters and return type void!
    /// </para>
    /// </summary>
    /// <typeparam name="TException">The type of exception expected.</typeparam>
    /// <param name="methodToExecute">The method to execute.</param>
    public static void Throws<TException>(Action methodToExecute) where TException : System.Exception
    {
        try
        {
            methodToExecute();
        }
        catch (Exception e)
        {
            Assert.IsTrue(e.GetType() == typeof(TException), "Expected exception of type " + typeof(TException) + " but type of " + e.GetType() + " was thrown instead.");
            return;
        }
        Assert.Fail("Expected exception of type " + typeof(TException) + " but no exception was thrown.");
    }

В тестовом модуле я могу написать:

ExceptionAssert.Throws<ArgumentNullException>(theProxy.LoadProduct,productNumber); 

Теперь я хочу написать больше методов, которые принимают методы в качестве входных данных с аргументами и возвращаемыми значениями. Как я понимаю, универсальный Func должен служить этому. И подпись метода должна быть такой:

public static void Throws<TException>(Func<T, TResult> methodToExecute, T methodArgument) where TException : System.Exception

Но это не скомпилируется. Я всегда должен писать явный тип, такой как Func, а не универсальный. В чем дело? Должна быть возможность объявить это универсальным способом, потому что LINQ работает следующим образом.

EDIT:

Хорошая идея объявить все, а не только половину. Результат этого:

/// <summary>
/// Contains assertion types for exceptions that are not provided with the standard MSTest assertions.
/// </summary>
/// <typeparam name="T">The type of the input arguments.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <remarks>
/// The standard test framework has an Attribute called <see cref="ExpectedExceptionAttribute">ExpectedExceptionAttribute</see>. This attribute has two
/// main disadvantages:
/// <para>
/// 1. The unit test stops at the line which throws the expected exception. If you want to test a method which throws a bunch of exceptions
/// you must write a test for each exception.
/// 2. The attribute does not specify exactly where the exception has to be thrown. So if a method call earlier than expected throws
/// suddenly the same exception, the whole test is still o.k.
/// </para>
/// So this class can be used like the common assertions. You can test a method at a specific line in the test for a specific exception.
/// </remarks>
internal static class ExceptionAssert<T,TResult>
{
    /// <summary>
    /// Checks to make sure that the input delegate throws a exception of type TException.
    /// <para>
    /// The input delegate must be a method with no parameters and return type void!
    /// </para>
    /// </summary>
    /// <typeparam name="TException">The type of exception expected.</typeparam>
    /// <param name="methodToExecute">The method to execute.</param>
    public static void Throws<TException>(Action methodToExecute) where TException : System.Exception
    {
        try
        {
            methodToExecute();
        }
        catch (Exception e)
        {
            Assert.IsTrue(e.GetType() == typeof(TException), "Expected exception of type " + typeof(TException) + " but type of " + e.GetType() + " was thrown instead.");
            return;
        }
        Assert.Fail("Expected exception of type " + typeof(TException) + " but no exception was thrown.");
    }

    /// <summary>
    /// Checks to make sure that the input delegate throws a exception of type TException with a specific exception message.
    /// <para>
    /// The input delegate must be a method with no parameters and return type void!
    /// </para>
    /// </summary>
    /// <typeparam name="TException">The type of exception expected.</typeparam>
    /// <param name="expectedMessage">The expected exception message.</param>
    /// <param name="methodToExecute">The method to execute.</param>
    /// <remarks>
    /// This method asserts if the given message and the message of the thrown exception are not equal!
    /// </remarks>
    public static void Throws<TException>(string expectedMessage, Action methodToExecute) where TException : System.Exception
    {
        try
        {
            methodToExecute();
        }
        catch (Exception e)
        {
            Assert.IsTrue(e.GetType() == typeof(TException), "Expected exception of type " + typeof(TException) + " but type of " + e.GetType() + " was thrown instead.");
            Assert.AreEqual(expectedMessage, e.Message, "Expected exception with a message of '" + expectedMessage + "' but exception with message of '" + e.Message + "' was thrown instead.");
            return;
        }
        Assert.Fail("Expected exception of type " + typeof(TException) + " but no exception was thrown.");
    }


    /// <summary>
    /// Checks to make sure that the input delegate throws a exception of type TException with a specific exception message.
    /// <para>
    /// The input delegate must be a method with ONE parameter and return type!
    /// </para>
    /// </summary>
    /// <typeparam name="TException">The type of the exception.</typeparam>
    /// <param name="methodToExecute">The method to execute.</param>
    /// <param name="argument">The argument to input.</param>
    public static void Throws<TException>(Func<T,TResult> methodToExecute, T argument) 
        where TException : System.Exception
    {
        try
        {
            methodToExecute(argument);
        }
        catch (Exception e)
        {
            Assert.IsTrue(e.GetType() == typeof(TException), "Expected exception of type " + typeof(TException) + " but type of " + e.GetType() + " was thrown instead.");
            return;
        }
        Assert.Fail("Expected exception of type " + typeof(TException) + " but no exception was thrown.");
    }
}

Ответы [ 2 ]

4 голосов
/ 16 февраля 2009

Вы должны также "объявить" T и TResult, например,

public static void Throws<T, TResult, TException>(Func<T, TResult> methodToExecute, T methodArgument) where TException : System.Exception

в противном случае компилятор не знает, что такое T и TResult.

1 голос
/ 16 февраля 2009

Это компилируется:

 public static void Throws<TException, T, TResult>(Func<T, TResult> methodToExecute, T methodArgument) where TException : System.Exception

Это дает вам результат, который вы ищете?

...