Closed rosdi closed 8 years ago
Hi rosdi -
All I ever do is put the async keyword in the method signature and an await before the SUT Method (assuming that it is awaitable)
protected override **async** void When()
{
result = **await** SUT.MyAwesomeMethodAsync(param).Result;
}
Hi Chuck... but it is generally considered bad practice to return void on async method right? http://haacked.com/archive/2014/11/11/async-void-methods/
Maybe it is considered ok for SpecsFor scenario but I just want to make sure I am doing it the proper way here...
You know...I was just looking at that yesterday and did not apply that to my specs :)
I think that When would need to return a Task, right? I just noticed that many test runners will let you create a Test that Returns Task instead of void:
https://msdn.microsoft.com/en-us/magazine/dn818493.aspx
So, based on that article, I think you could do the following until there is a Task:
protected override void When()
{
Task.Run(async () =>
{
result = await SUT.MyAwesomeMethodAsync(param).Result;
}).GetAwaiter().GetResult();
}
It's a bit verbose.
In 99% of cases, you should be fine with just calling .Result on your async method. I've done that on many a project without issue.
I might add support for async directly in the future, perhaps after I get things running on NUnit 3....
Hi Matt,
This isn't an issue... I just want to know the correct way to test async method. Currently I call
.Result
like so...Is this the correct way?