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.
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.
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.
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.
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.
Please try mark method with:
[ExpectedException(typeof(NotImplementedException))]
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(); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With