Skip to content

Commit 457409c

Browse files
duracellkoegil
andcommitted
Add support for asynchronous Razor tests (#27)
* Add support for asyncrhonous Razor tests * Rename Fixture.AsyncTest to TestAsync * Update description of Fixture Co-Authored-By: Egil Hansen <egil@assimilated.dk>
1 parent e7b79ac commit 457409c

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

src/Components/Fixture.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Microsoft.AspNetCore.Components;
45

56
namespace Egil.RazorComponents.Testing
@@ -12,20 +13,29 @@ namespace Egil.RazorComponents.Testing
1213
public class Fixture : FragmentBase
1314
{
1415
private Action _setup = NoopTestMethod;
16+
private Func<Task> _setupAsync = NoopAsyncTestMethod;
1517
private Action _test = NoopTestMethod;
18+
private Func<Task> _testAsync = NoopAsyncTestMethod;
1619
private IReadOnlyCollection<Action> _tests = Array.Empty<Action>();
20+
private IReadOnlyCollection<Func<Task>> _testsAsync = Array.Empty<Func<Task>>();
1721

1822
/// <summary>
1923
/// A description or name for the test that will be displayed if the test fails.
2024
/// </summary>
2125
[Parameter] public string? Description { get; set; }
2226

2327
/// <summary>
24-
/// Gets or sets the setup action to perform before the <see cref="Test"/> action
25-
/// and <see cref="Tests"/> actions are invoked.
28+
/// Gets or sets the setup action to perform before the <see cref="Test"/> action,
29+
/// <see cref="TestAsync"/> action and <see cref="Tests"/> and <see cref="TestsAsync"/> actions are invoked.
2630
/// </summary>
2731
[Parameter] public Action Setup { get => _setup; set => _setup = value ?? NoopTestMethod; }
2832

33+
/// <summary>
34+
/// Gets or sets the asynchronous setup action to perform before the <see cref="Test"/> action,
35+
/// <see cref="TestAsync"/> action and <see cref="Tests"/> and <see cref="TestsAsync"/> actions are invoked.
36+
/// </summary>
37+
[Parameter] public Func<Task> SetupAsync { get => _setupAsync; set => _setupAsync = value ?? NoopAsyncTestMethod; }
38+
2939
/// <summary>
3040
/// Gets or sets the first test action to invoke, after the <see cref="Setup"/> action has
3141
/// executed (if provided).
@@ -35,6 +45,15 @@ public class Fixture : FragmentBase
3545
/// </summary>
3646
[Parameter] public Action Test { get => _test; set => _test = value ?? NoopTestMethod; }
3747

48+
/// <summary>
49+
/// Gets or sets the first test action to invoke, after the <see cref="SetupAsync"/> action has
50+
/// executed (if provided).
51+
///
52+
/// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s
53+
/// defined in the <see cref="Fixture"/>.
54+
/// </summary>
55+
[Parameter] public Func<Task> TestAsync { get => _testAsync; set => _testAsync = value ?? NoopAsyncTestMethod; }
56+
3857
/// <summary>
3958
/// Gets or sets the test actions to invoke, one at the time, in the order they are placed
4059
/// into the collection, after the <see cref="Setup"/> action and the <see cref="Test"/> action has
@@ -45,6 +64,18 @@ public class Fixture : FragmentBase
4564
/// </summary>
4665
[Parameter] public IReadOnlyCollection<Action> Tests { get => _tests; set => _tests = value ?? Array.Empty<Action>(); }
4766

67+
/// <summary>
68+
/// Gets or sets the test actions to invoke, one at the time, in the order they are placed
69+
/// into the collection, after the <see cref="SetupAsync"/> action and the <see cref="TestAsync"/> action has
70+
/// executed (if provided).
71+
///
72+
/// Use this to assert against the <see cref="ComponentUnderTest"/> and <see cref="Fragment"/>'s
73+
/// defined in the <see cref="Fixture"/>.
74+
/// </summary>
75+
[Parameter] public IReadOnlyCollection<Func<Task>> TestsAsync { get => _testsAsync; set => _testsAsync = value ?? Array.Empty<Func<Task>>(); }
76+
4877
private static void NoopTestMethod() { }
78+
79+
private static Task NoopAsyncTestMethod() => Task.CompletedTask;
4980
}
5081
}

src/Components/TestComponentBase.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Runtime.CompilerServices;
44
using System.Text;
5+
using System.Threading.Tasks;
56
using Egil.RazorComponents.Testing.Asserting;
67
using Egil.RazorComponents.Testing.Diffing;
78
using Microsoft.AspNetCore.Components;
@@ -52,12 +53,12 @@ public TestComponentBase()
5253
/// in the file and runs their associated tests.
5354
/// </summary>
5455
[Fact(DisplayName = "Razor test runner")]
55-
public void RazorTest()
56+
public async Task RazorTest()
5657
{
5758
var container = new ContainerComponent(_renderer.Value);
5859
container.Render(BuildRenderTree);
5960

60-
ExecuteFixtureTests(container);
61+
await ExecuteFixtureTests(container).ConfigureAwait(false);
6162
ExecuteSnapshotTests(container);
6263
}
6364

@@ -92,7 +93,7 @@ public override void WaitForNextRender(Action renderTrigger, TimeSpan? timeout =
9293
base.WaitForNextRender(renderTrigger, timeout);
9394
}
9495

95-
private void ExecuteFixtureTests(ContainerComponent container)
96+
private async Task ExecuteFixtureTests(ContainerComponent container)
9697
{
9798
foreach (var (_, fixture) in container.GetComponents<Fixture>())
9899
{
@@ -102,13 +103,20 @@ private void ExecuteFixtureTests(ContainerComponent container)
102103
_testContextAdapter.ActivateRazorTestContext(testData);
103104

104105
InvokeFixtureAction(fixture, fixture.Setup);
106+
await InvokeFixtureAction(fixture, fixture.SetupAsync).ConfigureAwait(false);
105107
InvokeFixtureAction(fixture, fixture.Test);
108+
await InvokeFixtureAction(fixture, fixture.TestAsync).ConfigureAwait(false);
106109

107110
foreach (var test in fixture.Tests)
108111
{
109112
InvokeFixtureAction(fixture, test);
110113
}
111114

115+
foreach (var test in fixture.TestsAsync)
116+
{
117+
await InvokeFixtureAction(fixture, test).ConfigureAwait(false);
118+
}
119+
112120
_testContextAdapter.DisposeActiveTestContext();
113121
}
114122
}
@@ -125,6 +133,18 @@ private static void InvokeFixtureAction(Fixture fixture, Action action)
125133
}
126134
}
127135

136+
private static async Task InvokeFixtureAction(Fixture fixture, Func<Task> action)
137+
{
138+
try
139+
{
140+
await action().ConfigureAwait(false);
141+
}
142+
catch (Exception ex)
143+
{
144+
throw new FixtureFailedException(fixture.Description ?? $"{action.Method.Name} failed:", ex);
145+
}
146+
}
147+
128148
private void ExecuteSnapshotTests(ContainerComponent container)
129149
{
130150
foreach (var (_, snapshot) in container.GetComponents<SnapshotTest>())

0 commit comments

Comments
 (0)