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

  • SEARCH
  • Home
  • 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 8679543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:59:51+00:00 2026-06-12T20:59:51+00:00

Does anyone have an example of how to unit test an async method in

  • 0

Does anyone have an example of how to unit test an async method in a Windows 8 Metro application, to ensure that it throws the required exception?

Given a class with an async method

public static class AsyncMathsStatic
{
    private const int DELAY = 500;

    public static async Task<int> Divide(int A, int B)
    {
        await Task.Delay(DELAY);
        if (B == 0)
            throw new DivideByZeroException();
        else
            return A / B;
    }
}

I want to write a test method using the new Async.ExpectsException construction. I’ve tried :-

[TestMethod]
public void DivideTest1()
{
    Assert.ThrowsException<DivideByZeroException>(async () => { int Result = await AsyncMathsStatic.Divide(4, 0); });
}

but of course the test doesn’t wait for the async method to complete, and so results in a failed test that the exception hasn’t been thrown.

  • 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-06-12T20:59:52+00:00Added an answer on June 12, 2026 at 8:59 pm

    You can use an async Task unit test with the regular ExpectedExceptionAttribute:

    [TestMethod]
    [ExpectedException(typeof(DivideByZeroException))]
    public async Task DivideTest1()
    {
      int Result = await AsyncMathsStatic.Divide(4, 0);
    }
    

    Update from comment: ExpectedExceptionAttribute on Win8 unit test projects has been replaced with Assert.ThrowsException, which is nicely undocumented AFAICT. This is a good change design-wise, but I don’t know why it’s only supported on Win8.

    Well, assuming that there’s no async-compatible Assert.ThrowsException (I can’t tell if there is one or not due to lack of documentation), you could build one yourself:

    public static class AssertEx
    {
      public async Task ThrowsExceptionAsync<TException>(Func<Task> code)
      {
        try
        {
          await code();
        }
        catch (Exception ex)
        {
          if (ex.GetType() == typeof(TException))
            return;
          throw new AssertFailedException("Incorrect type; expected ... got ...", ex);
        }
    
        throw new AssertFailedException("Did not see expected exception ...");
      }
    }
    

    and then use it as such:

    [TestMethod]
    public async Task DivideTest1()
    {
      await AssertEx.ThrowsException<DivideByZeroException>(async () => { 
          int Result = await AsyncMathsStatic.Divide(4, 0);
      });
    }
    

    Note that my example here is just doing an exact check for the exception type; you may prefer to allow descendant types as well.

    Update 2012-11-29: Opened a UserVoice suggestion to add this to Visual Studio.

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

Sidebar

Related Questions

Does anyone have a simple example of implementing an async validation rule in csla?
Does anyone have an example of unit testing in CSLA. I find it difficult
Does anyone know how to test Mongoose Validations? Example, I have the following Schema
Does anyone have any good links about a practical example of Unit of Work
Does anyone have an example of setting up Authlogic with a namespace in Rails?
Does anyone have any example on how to put a date time picker inside
Does anyone have an example of rendering a remote file in XTK (https://github.com/xtk/X)? Is
Question: Does anyone have an example of a filter as you type dropdown control
Does anyone have a smal example of how to programmatically, in c/c++, load a
Does anyone have a working example of a video player built using Qt phonon?

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.