From d73b90d0591bd7f52aa3ebaf76628a22dce49fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rastislav=20Novotn=C3=BD?= Date: Sat, 11 Jan 2020 12:06:15 +0800 Subject: [PATCH 1/3] Add support for asyncrhonous Razor tests --- src/Components/Fixture.cs | 35 +++++++++++++++++++++++++++-- src/Components/TestComponentBase.cs | 26 ++++++++++++++++++--- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/Components/Fixture.cs b/src/Components/Fixture.cs index d196ec371..6a21c5e27 100644 --- a/src/Components/Fixture.cs +++ b/src/Components/Fixture.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace Egil.RazorComponents.Testing @@ -12,8 +13,11 @@ namespace Egil.RazorComponents.Testing public class Fixture : FragmentBase { private Action _setup = NoopTestMethod; + private Func _asyncSetup = NoopAsyncTestMethod; private Action _test = NoopTestMethod; + private Func _asyncTest = NoopAsyncTestMethod; private IReadOnlyCollection _tests = Array.Empty(); + private IReadOnlyCollection> _asyncTests = Array.Empty>(); /// /// A description or name for the test that will be displayed if the test fails. @@ -21,11 +25,17 @@ public class Fixture : FragmentBase [Parameter] public string? Description { get; set; } /// - /// Gets or sets the setup action to perform before the action - /// and actions are invoked. + /// Gets or sets the setup action to perform before the action, + /// action and and actions are invoked. /// [Parameter] public Action Setup { get => _setup; set => _setup = value ?? NoopTestMethod; } + /// + /// Gets or sets the setup asynchronous action to perform before the action, + /// action and and actions are invoked. + /// + [Parameter] public Func AsyncSetup { get => _asyncSetup; set => _asyncSetup = value ?? NoopAsyncTestMethod; } + /// /// Gets or sets the first test action to invoke, after the action has /// executed (if provided). @@ -35,6 +45,15 @@ public class Fixture : FragmentBase /// [Parameter] public Action Test { get => _test; set => _test = value ?? NoopTestMethod; } + /// + /// Gets or sets the first test action to invoke, after the action has + /// executed (if provided). + /// + /// Use this to assert against the and 's + /// defined in the . + /// + [Parameter] public Func AsyncTest { get => _asyncTest; set => _asyncTest = value ?? NoopAsyncTestMethod; } + /// /// Gets or sets the test actions to invoke, one at the time, in the order they are placed /// into the collection, after the action and the action has @@ -45,6 +64,18 @@ public class Fixture : FragmentBase /// [Parameter] public IReadOnlyCollection Tests { get => _tests; set => _tests = value ?? Array.Empty(); } + /// + /// Gets or sets the test actions to invoke, one at the time, in the order they are placed + /// into the collection, after the action and the action has + /// executed (if provided). + /// + /// Use this to assert against the and 's + /// defined in the . + /// + [Parameter] public IReadOnlyCollection> AsyncTests { get => _asyncTests; set => _asyncTests = value ?? Array.Empty>(); } + private static void NoopTestMethod() { } + + private static Task NoopAsyncTestMethod() => Task.CompletedTask; } } diff --git a/src/Components/TestComponentBase.cs b/src/Components/TestComponentBase.cs index 2969e0031..8a3fa2d50 100644 --- a/src/Components/TestComponentBase.cs +++ b/src/Components/TestComponentBase.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Runtime.CompilerServices; using System.Text; +using System.Threading.Tasks; using Egil.RazorComponents.Testing.Asserting; using Egil.RazorComponents.Testing.Diffing; using Microsoft.AspNetCore.Components; @@ -52,12 +53,12 @@ public TestComponentBase() /// in the file and runs their associated tests. /// [Fact(DisplayName = "Razor test runner")] - public void RazorTest() + public async Task RazorTest() { var container = new ContainerComponent(_renderer.Value); container.Render(BuildRenderTree); - ExecuteFixtureTests(container); + await ExecuteFixtureTests(container).ConfigureAwait(false); ExecuteSnapshotTests(container); } @@ -92,7 +93,7 @@ public override void WaitForNextRender(Action renderTrigger, TimeSpan? timeout = base.WaitForNextRender(renderTrigger, timeout); } - private void ExecuteFixtureTests(ContainerComponent container) + private async Task ExecuteFixtureTests(ContainerComponent container) { foreach (var (_, fixture) in container.GetComponents()) { @@ -102,13 +103,20 @@ private void ExecuteFixtureTests(ContainerComponent container) _testContextAdapter.ActivateRazorTestContext(testData); InvokeFixtureAction(fixture, fixture.Setup); + await InvokeFixtureAction(fixture, fixture.AsyncSetup).ConfigureAwait(false); InvokeFixtureAction(fixture, fixture.Test); + await InvokeFixtureAction(fixture, fixture.AsyncTest).ConfigureAwait(false); foreach (var test in fixture.Tests) { InvokeFixtureAction(fixture, test); } + foreach (var test in fixture.AsyncTests) + { + await InvokeFixtureAction(fixture, test).ConfigureAwait(false); + } + _testContextAdapter.DisposeActiveTestContext(); } } @@ -125,6 +133,18 @@ private static void InvokeFixtureAction(Fixture fixture, Action action) } } + private static async Task InvokeFixtureAction(Fixture fixture, Func action) + { + try + { + await action().ConfigureAwait(false); + } + catch (Exception ex) + { + throw new FixtureFailedException(fixture.Description ?? $"{action.Method.Name} failed:", ex); + } + } + private void ExecuteSnapshotTests(ContainerComponent container) { foreach (var (_, snapshot) in container.GetComponents()) From cc85bafc7ab7d620c56d7cef54c2d4de708baafe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rastislav=20Novotn=C3=BD?= Date: Tue, 7 Jan 2020 21:01:47 +0800 Subject: [PATCH 2/3] Rename Fixture.AsyncTest to TestAsync --- src/Components/Fixture.cs | 20 ++++++++++---------- src/Components/TestComponentBase.cs | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Components/Fixture.cs b/src/Components/Fixture.cs index 6a21c5e27..66263c885 100644 --- a/src/Components/Fixture.cs +++ b/src/Components/Fixture.cs @@ -13,11 +13,11 @@ namespace Egil.RazorComponents.Testing public class Fixture : FragmentBase { private Action _setup = NoopTestMethod; - private Func _asyncSetup = NoopAsyncTestMethod; + private Func _setupAsync = NoopAsyncTestMethod; private Action _test = NoopTestMethod; - private Func _asyncTest = NoopAsyncTestMethod; + private Func _testAsync = NoopAsyncTestMethod; private IReadOnlyCollection _tests = Array.Empty(); - private IReadOnlyCollection> _asyncTests = Array.Empty>(); + private IReadOnlyCollection> _testsAsync = Array.Empty>(); /// /// A description or name for the test that will be displayed if the test fails. @@ -26,15 +26,15 @@ public class Fixture : FragmentBase /// /// Gets or sets the setup action to perform before the action, - /// action and and actions are invoked. + /// action and and actions are invoked. /// [Parameter] public Action Setup { get => _setup; set => _setup = value ?? NoopTestMethod; } /// /// Gets or sets the setup asynchronous action to perform before the action, - /// action and and actions are invoked. + /// action and and actions are invoked. /// - [Parameter] public Func AsyncSetup { get => _asyncSetup; set => _asyncSetup = value ?? NoopAsyncTestMethod; } + [Parameter] public Func SetupAsync { get => _setupAsync; set => _setupAsync = value ?? NoopAsyncTestMethod; } /// /// Gets or sets the first test action to invoke, after the action has @@ -46,13 +46,13 @@ public class Fixture : FragmentBase [Parameter] public Action Test { get => _test; set => _test = value ?? NoopTestMethod; } /// - /// Gets or sets the first test action to invoke, after the action has + /// Gets or sets the first test action to invoke, after the action has /// executed (if provided). /// /// Use this to assert against the and 's /// defined in the . /// - [Parameter] public Func AsyncTest { get => _asyncTest; set => _asyncTest = value ?? NoopAsyncTestMethod; } + [Parameter] public Func TestAsync { get => _testAsync; set => _testAsync = value ?? NoopAsyncTestMethod; } /// /// Gets or sets the test actions to invoke, one at the time, in the order they are placed @@ -66,13 +66,13 @@ public class Fixture : FragmentBase /// /// Gets or sets the test actions to invoke, one at the time, in the order they are placed - /// into the collection, after the action and the action has + /// into the collection, after the action and the action has /// executed (if provided). /// /// Use this to assert against the and 's /// defined in the . /// - [Parameter] public IReadOnlyCollection> AsyncTests { get => _asyncTests; set => _asyncTests = value ?? Array.Empty>(); } + [Parameter] public IReadOnlyCollection> TestsAsync { get => _testsAsync; set => _testsAsync = value ?? Array.Empty>(); } private static void NoopTestMethod() { } diff --git a/src/Components/TestComponentBase.cs b/src/Components/TestComponentBase.cs index 8a3fa2d50..85e0d70ea 100644 --- a/src/Components/TestComponentBase.cs +++ b/src/Components/TestComponentBase.cs @@ -103,16 +103,16 @@ private async Task ExecuteFixtureTests(ContainerComponent container) _testContextAdapter.ActivateRazorTestContext(testData); InvokeFixtureAction(fixture, fixture.Setup); - await InvokeFixtureAction(fixture, fixture.AsyncSetup).ConfigureAwait(false); + await InvokeFixtureAction(fixture, fixture.SetupAsync).ConfigureAwait(false); InvokeFixtureAction(fixture, fixture.Test); - await InvokeFixtureAction(fixture, fixture.AsyncTest).ConfigureAwait(false); + await InvokeFixtureAction(fixture, fixture.TestAsync).ConfigureAwait(false); foreach (var test in fixture.Tests) { InvokeFixtureAction(fixture, test); } - foreach (var test in fixture.AsyncTests) + foreach (var test in fixture.TestsAsync) { await InvokeFixtureAction(fixture, test).ConfigureAwait(false); } From 838b0ee1dd222c2847aa79af86fe154930da7005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rastislav=20Novotn=C3=BD?= Date: Wed, 8 Jan 2020 09:19:52 +0800 Subject: [PATCH 3/3] Update description of Fixture Co-Authored-By: Egil Hansen --- src/Components/Fixture.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Fixture.cs b/src/Components/Fixture.cs index 66263c885..4785235b6 100644 --- a/src/Components/Fixture.cs +++ b/src/Components/Fixture.cs @@ -31,7 +31,7 @@ public class Fixture : FragmentBase [Parameter] public Action Setup { get => _setup; set => _setup = value ?? NoopTestMethod; } /// - /// Gets or sets the setup asynchronous action to perform before the action, + /// Gets or sets the asynchronous setup action to perform before the action, /// action and and actions are invoked. /// [Parameter] public Func SetupAsync { get => _setupAsync; set => _setupAsync = value ?? NoopAsyncTestMethod; }