machine / machine.specifications

Machine.Specifications is a Context/Specification framework for .NET that removes language noise and simplifies tests.
MIT License
885 stars 178 forks source link

Support for async/await specifications #426

Closed robertcoltheart closed 3 years ago

robertcoltheart commented 3 years ago

Fixed #293

This is inspired by the xUnit strategy for executing async void methods.

This lets you run specs using the following syntax:

class when_using_async
{
    Establish context = async () =>
        await Task.Delay(10);

    Because of = async () =>
        await Task.Delay(10);

    It should_invoke_async = async () =>
        await Task.Delay(10);

    Cleanup after = async () =>
        await Task.Delay(10);
}
mikeblakeuk commented 3 years ago

AMAZING. Happy to try out the preview if you publish it to nuget.

mikeblakeuk commented 3 years ago

Using Rider 2020.3

I assume this is because I am mixing versions

    <PackageReference Include="Machine.Specifications" Version="1.0.1-PullRequest0426.255" />
    <PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.10.1" />

gives:

  TestRunner: Application  JetBrains.Rd.Tasks.RdFault: Constructor on type 'Machine.Specifications.Runner.Impl.DefaultRunner' not found., reason: System.MissingMethodException: Constructor on type 'Machine.Specifications.Runner.Impl.DefaultRunner' not found.

  Server stack trace: 
     at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
     at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
     at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
     at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
     at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
     at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
     at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
     at Machine.Specifications.Runner.Utility.AppDomainRunner.CreateRunnerInSeparateAppDomain(AppDomain appDomain, AssemblyPath assembly)
robertcoltheart commented 3 years ago

@mikeblakeuk This was a bug in the Rider MSpec plugin. Update your plugin and it should work.

mikeblakeuk commented 3 years ago

image Looks like net 4.6.2 tests don't work but net 5.0 ones do...

In VS with net 4.6.2, (4.7.2 & 5.0 works)

System.Runtime.Serialization.SerializationException: Type 'Machine.Specifications.ExceptionResult' in Assembly 'Machine.Specifications, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

robertcoltheart commented 3 years ago

Ignore the error, this has to be back-ported into 1.0 for it to work. Will release off the 1.0.0 branch.

mikeblakeuk commented 3 years ago

I assume a version will appear on NuGet soon image https://www.nuget.org/packages/Machine.Specifications/

robertcoltheart commented 3 years ago

Released as v1.1.0-beta.1, try it out and let me know how it goes.

mikeblakeuk commented 3 years ago

image

Looks like you can't mix sync with async

mikeblakeuk commented 3 years ago

Could be the version of MSpec i was using, sorry (I was using a pull request version!)

robertcoltheart commented 3 years ago

Ok, please confirm if this works with the beta. There is a unit test that covers this, so it should work.