Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assert that a C# async method throws an exception in a unit test? [duplicate]

Possible Duplicate:
How do I test an async method with NUnit, eventually with another framework?

What I would like to know is how I can assert that an asynchronous method throws an exception, in a C# unit test? I am able to write asynchronous unit tests with Microsoft.VisualStudio.TestTools.UnitTesting in Visual Studio 2012, but have not figured out how to test for exceptions. I know that xUnit.net also supports asynchronous test methods in this way, although I haven't tried that framework yet.

For an example of what I mean, the following code defines the system under test:

using System;
using System.Threading.Tasks;

public class AsyncClass
{
    public AsyncClass() { }

    public Task<int> GetIntAsync()
    {
        throw new NotImplementedException();
    }
}    

This code snippet defines a test TestGetIntAsync for AsyncClass.GetIntAsync. This is where I need input on how to accomplish the assertion that GetIntAsync throws an exception:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;

[TestClass]
public class TestAsyncClass
{
    [TestMethod]
    public async Task TestGetIntAsync()
    {
        var obj = new AsyncClass();
        // How do I assert that an exception is thrown?
        var rslt = await obj.GetIntAsync();
    }
}

Feel free to employ some other relevant unit test framework than the Visual Studio one, such as xUnit.net, if necessary or you would argue that it's a better option.

like image 626
aknuds1 Avatar asked Sep 03 '12 13:09

aknuds1


People also ask

What does assert () do in C?

In the C Programming Language, assert is a macro that is designed to be used like a function. It checks the value of an expression that we expect to be true under normal circumstances. If expression is a nonzero value, the assert macro does nothing.

What is assert 0 C?

assert(0) or assert(false) is usually used to mark unreachable code, so that in debug mode a diagnostic message is emitted and the program is aborted when the supposedly unreachable is actually reached, which is a clear signal that the program isn't doing what we think it is.

Where is assert defined in C?

August 2022) assert. h is a header file in the standard library of the C programming language that defines the C preprocessor macro assert() . In C++ it is also available through the <cassert> header file.

Should I use assert in C?

You should only use assert to check for situations that "can't happen", e.g. that violate the invariants or postconditions of an algorithm, but probably not for input validation (certainly not in libraries). When detecting invalid input from clients, be friendly and return an error code.


2 Answers

Please try mark method with:

[ExpectedException(typeof(NotImplementedException))]
like image 162
petro.sidlovskyy Avatar answered Oct 16 '22 08:10

petro.sidlovskyy


1st Option would be:

try
{
   await obj.GetIntAsync();
   Assert.Fail("No exception was thrown");
}
catch (NotImplementedException e)
{      
   Assert.Equal("Exception Message Text", e.Message);
}

2nd Option would be to use the Expected Exception Attribute:

[ExpectedException(typeof(NotImplementedException))]

3rd Option would be to use the Assert.Throws :

Assert.Throws<NotImplementedException>(delegate { obj.GetIntAsync(); });
like image 21
CloudyMarble Avatar answered Oct 16 '22 09:10

CloudyMarble