Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specflow steps with await for async API

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?

like image 847
2ooom Avatar asked Oct 14 '16 00:10

2ooom


People also ask

What is the difference between Sync and await?

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.

What is the async await pattern?

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.

How does async await work?

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.

How does async await work in Web API?

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.


1 Answers

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.

like image 198
Sam Holder Avatar answered Sep 20 '22 19:09

Sam Holder