Skip to content

Commit

Permalink
Merge pull request #647 from ocsurfnut/RestoreAsyncSupport
Browse files Browse the repository at this point in the history
#542 - Restore Async Step Method Support - Await Task Before Continuing
  • Loading branch information
gasparnagy committed Jul 14, 2016
2 parents 80e5fc0 + 7452c90 commit 194d0b3
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 203 deletions.
14 changes: 14 additions & 0 deletions Runtime/Bindings/BindingInvoker.cs
Expand Up @@ -14,6 +14,8 @@

namespace TechTalk.SpecFlow.Bindings
{
using System.Threading.Tasks;

public class BindingInvoker : IBindingInvoker
{
protected readonly RuntimeConfiguration runtimeConfiguration;
Expand Down Expand Up @@ -43,6 +45,12 @@ public virtual object InvokeBinding(IBinding binding, IContextManager contextMan
Array.Copy(arguments, 0, invokeArgs, 1, arguments.Length);
invokeArgs[0] = contextManager;
result = bindingAction.DynamicInvoke(invokeArgs);

if (result is Task)
{
((Task)result).Wait();
}

stopwatch.Stop();
}

Expand All @@ -64,6 +72,12 @@ public virtual object InvokeBinding(IBinding binding, IContextManager contextMan
ex = ex.PreserveStackTrace(errorProvider.GetMethodText(binding.Method));
throw ex;
}
catch (AggregateException aggregateEx)
{
var ex = aggregateEx.InnerExceptions.First();
ex = ex.PreserveStackTrace(errorProvider.GetMethodText(binding.Method));
throw ex;
}
}

protected virtual CultureInfoScope CreateCultureInfoScope(IContextManager contextManager)
Expand Down
59 changes: 57 additions & 2 deletions Tests/RuntimeTests/StepExecutionTests.cs
@@ -1,8 +1,8 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Rhino.Mocks;
using TechTalk.SpecFlow.Infrastructure;
using TestStatus = TechTalk.SpecFlow.Infrastructure.TestStatus;

namespace TechTalk.SpecFlow.RuntimeTests
Expand Down Expand Up @@ -75,6 +75,13 @@ public virtual void DistinguishByTableParam2(Table table)
{

}


[Given("Returns a Task")]
public virtual Task ReturnsATask()
{
throw new NotSupportedException("should be mocked");
}
}

[Binding]
Expand Down Expand Up @@ -275,6 +282,54 @@ public void SholdRaiseBindingErrorIfWrongParamNumber()
MockRepository.VerifyAll();
}

[Test]
public void SholdCallBindingThatReturnsTask()
{
StepExecutionTestsBindings bindingInstance;
TestRunner testRunner = GetTestRunnerFor(out bindingInstance);

bool taskFinished = false;

bindingInstance.Expect(b => b.ReturnsATask()).Return(Task.Factory.StartNew(() =>
{
Thread.Sleep(800);
taskFinished = true;
}));

MockRepository.ReplayAll();

testRunner.Given("Returns a Task");
Assert.IsTrue(taskFinished);
Assert.AreEqual(TestStatus.OK, GetLastTestStatus());
MockRepository.VerifyAll();
}

[Test]
public void SholdCallBindingThatReturnsTaskAndReportError()
{
StepExecutionTestsBindings bindingInstance;
TestRunner testRunner = GetTestRunnerFor(out bindingInstance);

bool taskFinished = false;

bindingInstance.Expect(b => b.ReturnsATask()).Return(Task.Factory.StartNew(() =>
{
Thread.Sleep(800);
taskFinished = true;
throw new Exception("catch meee");
}));

MockRepository.ReplayAll();

testRunner.Given("Returns a Task");
Assert.IsTrue(taskFinished);
Assert.AreEqual(TestStatus.TestError, GetLastTestStatus());
Assert.AreEqual("catch meee", ContextManagerStub.ScenarioContext.TestError.Message);

MockRepository.VerifyAll();
}



}
}
3 changes: 1 addition & 2 deletions Tests/TechTalk.SpecFlow.Specs/App.config
Expand Up @@ -3,12 +3,11 @@
<configSections>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
</configSections>

<appSettings>
<add key="testProjectFolder" value="SpecFlowTestProject" />
</appSettings>
<specFlow>
<unitTestProvider name="NUnit" />
<generator allowRowTests="false" />
<!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --></specFlow>
</specFlow>
</configuration>

0 comments on commit 194d0b3

Please sign in to comment.