We're trying to get the following scenrio step to break the test in case failure happens within DoAyncStuff()
method:
[Given(@"There is something")]
public async Task GivenSomething()
{
await DoStuff();
}
private async Task DoStuff()
{
await Task.Run(() => Thread.Sleep(1000));
throw new ApplicationException("Boom");
}
But it actually makes a happy green-run until you use .Wait()
or .Result
:
[Given(@"There is something")]
public void GivenSomething()
{
DoStuff().Wait();
}
The problem seems to be in the NUnit generated-spec which looks like this:
public virtual void SomethingAsync()
{
...
testRunner.Given("There is something", ...);
...
}
which seems to work with the following code:
public virtual async Task SomethingAsync()
{
...
await this.ScenarioSetup(scenarioInfo);
...
}
The code above is manually edited auto-generated file, so I'm actually looking for a way to produce following code automatically.
The documentation seems to be the only option available for asyncronous API but it's actually for Silverlight and as far as I understand uses some kind of API, while we'd preffer to use native C# await keyword.
Is there a way to handle natively async/await
is SpecFlow steps?
await can only be used in async functions. It is used for calling an async function and waits for it to resolve or reject. await blocks the execution of the code within the async function in which it is located.
In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function.
async and awaitInside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.
The async/await feature solves three performance or scalability problems: They can make your application handle more users. If you have requests that access an external resource such as a database or a web API then async frees up the thread while it is waiting.
In the current released version (2.1) version there is no support for async and await, support was added (via this merged PR) in v2.2 which is available from the CI server, but there is not an official release yet.
[EDIT]
2.2 has been released and supports async await in tests.
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