|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Shouldly; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Bunit.RazorTesting |
| 10 | +{ |
| 11 | + public class FixtureBaseTest : TestContext |
| 12 | + { |
| 13 | + private class FixtureComponent : FixtureBase<FixtureComponent> |
| 14 | + { |
| 15 | + protected override Task Run() => Run(this); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact(DisplayName = "Setup, SetupAsync and Test methods are called in the correct order")] |
| 19 | + public async Task Test001() |
| 20 | + { |
| 21 | + var callLog = new List<string>(3); |
| 22 | + var cut = RenderComponent<FixtureComponent>(builder => builder |
| 23 | + .Add(p => p.Setup, Setup) |
| 24 | + .Add(p => p.SetupAsync, SetupAsync) |
| 25 | + .Add(p => p.Test, Test) |
| 26 | + .AddChildContent("FOO") |
| 27 | + ); |
| 28 | + |
| 29 | + await cut.Instance.RunTest(); |
| 30 | + |
| 31 | + callLog[0].ShouldBe(nameof(Setup)); |
| 32 | + callLog[1].ShouldBe(nameof(SetupAsync)); |
| 33 | + callLog[2].ShouldBe(nameof(Test)); |
| 34 | + |
| 35 | + void Setup(FixtureComponent fixture) => callLog.Add(nameof(Setup)); |
| 36 | + Task SetupAsync(FixtureComponent fixture) |
| 37 | + { callLog.Add(nameof(SetupAsync)); return Task.CompletedTask; } |
| 38 | + void Test(FixtureComponent fixture) => callLog.Add(nameof(Test)); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact(DisplayName = "Setup, SetupAsync and TestAsync methods are called in the correct order")] |
| 42 | + public async Task Test002() |
| 43 | + { |
| 44 | + var callLog = new List<string>(3); |
| 45 | + var cut = RenderComponent<FixtureComponent>(builder => builder |
| 46 | + .Add(p => p.Setup, Setup) |
| 47 | + .Add(p => p.SetupAsync, SetupAsync) |
| 48 | + .Add(p => p.TestAsync, TestAsync) |
| 49 | + .AddChildContent("FOO") |
| 50 | + ); |
| 51 | + |
| 52 | + await cut.Instance.RunTest(); |
| 53 | + |
| 54 | + callLog[0].ShouldBe(nameof(Setup)); |
| 55 | + callLog[1].ShouldBe(nameof(SetupAsync)); |
| 56 | + callLog[2].ShouldBe(nameof(TestAsync)); |
| 57 | + |
| 58 | + void Setup(FixtureComponent fixture) => callLog.Add(nameof(Setup)); |
| 59 | + Task SetupAsync(FixtureComponent fixture) |
| 60 | + { callLog.Add(nameof(SetupAsync)); return Task.CompletedTask; } |
| 61 | + Task TestAsync(FixtureComponent fixture) |
| 62 | + { callLog.Add(nameof(TestAsync)); return Task.CompletedTask; } |
| 63 | + } |
| 64 | + |
| 65 | + [Fact(DisplayName = "Run fails when no ChildContent is provided")] |
| 66 | + public void Test010() |
| 67 | + { |
| 68 | + var cut = RenderComponent<FixtureComponent>(builder => builder |
| 69 | + .Add(p => p.Test, _ => { }) |
| 70 | + ); |
| 71 | + |
| 72 | + Should.Throw<ArgumentException>(() => cut.Instance.RunTest()) |
| 73 | + .ParamName.ShouldBe(nameof(FixtureComponent.ChildContent)); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact(DisplayName = "Run fails when both Test or TestAsync is missing")] |
| 77 | + public void Test011() |
| 78 | + { |
| 79 | + var cut = RenderComponent<FixtureComponent>(builder => builder |
| 80 | + .AddChildContent("FOO") |
| 81 | + ); |
| 82 | + |
| 83 | + Should.Throw<ArgumentException>(() => cut.Instance.RunTest()) |
| 84 | + .ParamName.ShouldBe(nameof(FixtureComponent.Test)); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact(DisplayName = "Run fails when both Test or TestAsync is missing")] |
| 88 | + public void Test012() |
| 89 | + { |
| 90 | + var cut = RenderComponent<FixtureComponent>(builder => builder |
| 91 | + .Add(p => p.Test, _ => { }) |
| 92 | + .Add(p => p.TestAsync, _ => Task.CompletedTask) |
| 93 | + .AddChildContent("FOO") |
| 94 | + ); |
| 95 | + |
| 96 | + Should.Throw<ArgumentException>(() => cut.Instance.RunTest()) |
| 97 | + .ParamName.ShouldBe(nameof(FixtureComponent.Test)); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments