/// <summary> /// Replicates <c>DoesNotThrowOrInconclusiveAsync</c> while allowing /// NUnit <see cref="ResultStateException"/>s to bubble up to the test framework. /// </summary> private static async Task DoesNotThrowOrInconclusiveAsync(AsyncTestDelegate code) { try { await code.Invoke(); } catch (Exception e) when(e is not ResultStateException) { Assert.Fail($"Expected: No Exception to be thrown\nBut was: {e}"); } }
/// <summary> /// Verifies that an async delegate throws a particular exception when called. /// </summary> /// <param name="expression">A constraint to be satisfied by the exception</param> /// <param name="code">A TestSnippet delegate</param> /// <param name="message">The message that will be displayed on failure</param> /// <param name="args">Arguments to be used in formatting the message</param> public static Exception ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, string message, params object[] args) { Exception caughtException = null; try { AwaitUtils.Await(code, code.Invoke()); } catch (Exception e) { caughtException = e; } Assert.That(caughtException, expression, message, args); return(caughtException); }
/// <summary> /// Fill-in for NUnit's Assert.ThrowsAsync /// Source: https://forum.unity.com/threads/can-i-replace-upgrade-unitys-nunit.488580/#post-6543523 /// </summary> /// <param name="code"></param> /// <param name="message"></param> /// <param name="args"></param> /// <typeparam name="TActual"></typeparam> /// <returns></returns> /// <exception cref="Exception"></exception> public static TActual AssertThrowsAsync <TActual>(AsyncTestDelegate code, string message = "", params object[] args) where TActual : Exception { return(Assert.Throws <TActual>(() => { try { code.Invoke().Wait(); // Will wrap any exceptions in an AggregateException } catch (AggregateException e) { if (e.InnerException is null) { throw; } throw e.InnerException; // Throw the unwrapped exception } }, message, args)); }
/// <summary> /// Executes the code and returns success if an exception is thrown. /// </summary> /// <param name="actual">A delegate representing the code to be tested</param> /// <returns>True if an exception is thrown, otherwise false</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { TestDelegate code = actual as TestDelegate; Exception caughtException = null; if (code != null) { #if ASYNC if (AwaitUtils.IsAsyncVoid(code)) { throw new ArgumentException("'async void' methods are not supported. Please use 'async Task' instead."); } #endif try { code(); } catch (Exception ex) { caughtException = ex; } } #if ASYNC AsyncTestDelegate asyncCode = actual as AsyncTestDelegate; if (asyncCode != null) { try { AwaitUtils.Await(asyncCode, asyncCode.Invoke()); } catch (Exception ex) { caughtException = ex; } } if (code == null && asyncCode == null) #else else #endif { throw new ArgumentException(string.Format("The actual value must be a TestDelegate or AsyncTestDelegate but was {0}", actual.GetType().Name), "actual"); } return(new ThrowsExceptionConstraintResult(this, caughtException)); }
public Task OneTimeSetUp() => _asyncUserCode.Invoke();
public Task TearDown() => _asyncUserCode.Invoke();
public Task SetUp() => _asyncUserCode.Invoke();
public Task TestMethod() => _asyncUserCode.Invoke();
public Task OneTimeTearDown() => _asyncUserCode.Invoke();
public static void DoesNotThrowAsync(AsyncTestDelegate action) { action.Invoke().Wait(); }