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 546791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:54:15+00:00 2026-05-13T10:54:15+00:00

So I find myself writing this code all the time: [TestMethod] [Description(Asserts that an

  • 0

So I find myself writing this code all the time:


[TestMethod]
[Description("Asserts that an ArgumentNullException is thrown if ResetPassword(null) is called")]
public void ResetPassword_Throws_ArgumentNullException_With_Null_Parameter( )
{
    try
    {
        new MembershipServiceProvider( ).ResetPassword( null );
    }
    catch ( ArgumentNullException )
    {
        // ArgumentNullException was expected
        Assert.IsTrue( true );
    }
    catch
    {
        Assert.Fail( "ArgumentNullException was expected" );
    }
}

So instead of writing this code over and over I’d really like to create a method which accepts a Lambda expression which will execute the method in the try block.

Something like this:


public void AssertExpectedException( Action theAction ) where TException : Exception
{
    try
    {
        // Execute the method here
    }
    catch ( TException )
    {
        Assert.IsTrue( true );
    }
    catch
    {
        Assert.Fail( string.Format( "An exception of type {0} was expected", typeof( TException ) ) );
    }
}

So I can do something like this:


var provider = new MembershipServiceProvider();
AssertExpectedException(provider => provider.ResetPassword(null));

I’m really not sure if any of this is on the right track but hopefully someone can point me in the right direction.

Thanks

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-13T10:54:15+00:00Added an answer on May 13, 2026 at 10:54 am

    You’re almost there. Here’s what the test helper should look like:

    public void AssertExpectedException<TException>( Action theAction )
        where TException : Exception 
    { 
        try 
        { 
            // Execute the method here 
            theAction();
        } 
        catch ( TException ) 
        { 
            // The Assert here is not necessary
        } 
        catch 
        { 
            Assert.Fail( string.Format(
                "An exception of type {0} was expected",
                typeof(TException))); 
        } 
    } 
    

    And to call it:

    var provider = new MembershipServiceProvider(); 
    AssertExpectedException<ArgumentNullException>(() => provider.ResetPassword(null)); 
    

    Note the usage of () => something which means the lambda has no parameters. You also have to specify the generic argument of ArgumentNullException because the compiler cannot infer it.

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

Sidebar

Related Questions

I find myself writing code that looks like this a lot: set<int> affected_items; while
I frequently find myself writing code like this: List<int> list = new List<int> {
This is the second time I found myself writing this kind of code, and
I find myself writing a lot of functions in PHP that return HTML code.
I often find myself writing a property that is evaluated lazily. Something like: if
In my day-to-day work, I often find myself writing classes like in this simplified
I find myself writing delegates occasionally for really simple functions (take no arguments and
I'm just starting to pick up ASP.Net MVC and find myself writing a lot
Often I find myself interacting with files in some way but after writing the
Greetings, Being new to jQuery's syntax, I find myself being a bit thrown off

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.