GithubHelp home page GithubHelp logo

Comments (2)

Evangelink avatar Evangelink commented on June 10, 2024

Hi @gao-artur,

Thanks for the suggestion. I am not sure to fully understand what you would like to be able to do. At the moment, all ThrowsExceptionAsync gives you a Task<TException> so awaiting that gives you your exception.

from testfx.

gao-artur avatar gao-artur commented on June 10, 2024

Hi @Evangelink. When thrown exception is different from the expected, it may be helpful to get the actual exception to create the error message. For example, you expect to get OperationCancellaedException, but RpcException is thrown instead. RpcException has a few properties with useful information, like Trailers for example. You would want to use this additional data in the error message (second argument in ThrowsExceptionAsync method). Currently, the framework will create an error message based on the user message, exception type, message, and stack trace.

if (!typeof(T).Equals(ex.GetType()))
{
    userMessage = BuildUserMessage(message, parameters);
    finalMessage = string.Format(
        CultureInfo.CurrentCulture,
        FrameworkMessages.WrongExceptionThrown,
        userMessage,
        typeof(T).Name,
        ex.GetType().Name,
        ex.Message,
        ex.StackTrace);
    ThrowAssertFailed("Assert.ThrowsException", finalMessage);
}

I suggest passing the exception to the user message builder. Note the Func<Exception?, string> messageBuilder instead of string message

public static async Task<T> ThrowsExceptionAsync<T>(Func<Task> action, Func<Exception?, string> messageBuilder, params object?[]? parameters)
    where T : Exception
{
    ...
    string userMessage, finalMessage;
    try
    {
        await action().ConfigureAwait(false);
    }
    catch (Exception ex)
    {
        if (!typeof(T).Equals(ex.GetType()))
        {
            userMessage = BuildUserMessage(messageBuilder, ex, parameters);
            ...
            ThrowAssertFailed("Assert.ThrowsException", finalMessage);
        }

        return (T)ex;
    }

    userMessage = BuildUserMessage(messageBuilder, exception: null, parameters);
    ...
    ThrowAssertFailed("Assert.ThrowsException", finalMessage);
    ...
}

Note the Func<Exception?, string> messageBuilder instead of string? format

internal static string BuildUserMessage(Func<Exception?, string> messageBuilder, Exception? exception, params object?[]? parameters)
{
    ...
    var format = messageBuilder(exception);

    return parameters == null || parameters.Length == 0
        ? ReplaceNulls(format)
        : string.Format(CultureInfo.CurrentCulture, ReplaceNulls(format), parameters);
}

from testfx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.