Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:41:07+00:00 2026-05-11T06:41:07+00:00

Hy, I’m writing a class which should help me in unit tests. The class

  • 0

Hy,

I’m writing a class which should help me in unit tests. The class offers methods to perform Assertions on Exceptions.

Until now I was able to write methods which take a function with no parameters and no return value as input. To do this I use the System.Action – delegates. My class looks like this:

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.');     } 

In the unit test I can write now:

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

Now I want to write more methods, which take methods as input with arguments and return values. As I understand the generic Func should serve this. And the method signature should be like this:

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

But this will not compile. I always have to write an explicit type like Func and not the generic. What’s wrong? It should be possible to declare it the generic way, because LINQ works like this.

EDIT:

It is a good idea to declare everything, not just the half. The result of it:

/// <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.');     } } 
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:41:08+00:00Added an answer on May 11, 2026 at 6:41 am

    You should ‘declare’ T and TResult as well, e.g.

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

    otherwise the compiler does not know what T and TResult are.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer use 'having' SELECT p.name, avg(r.rating) as average FROM products p… May 11, 2026 at 6:11 pm
  • Editorial Team
    Editorial Team added an answer The issue has been resolved as my error. The callbacks… May 11, 2026 at 6:11 pm
  • Editorial Team
    Editorial Team added an answer There are basically two ways of doing this: exceptions and… May 11, 2026 at 6:11 pm

Related Questions

Hy, I'm developing a Grails app which has to communicate with an existing Java
Hy, I'm writing a class which should help me in unit tests. The class
Hy all, I believe that the following piece of code is generating memory leak?
Hy, as we all know, developing a multithreading application is a hard thing. Especially
I'm trying to load a combo with an enumerator from Translation API. But, I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.