Skip to content

Commit fec96b4

Browse files
committed
Reaaranged test to match position of test targets
1 parent 6d62d6f commit fec96b4

16 files changed

Lines changed: 188 additions & 191 deletions

src/bunit.web.tests/ComponentTestFixtureTest.cs renamed to src/bunit.core.tests/ComponentParameterFactoryTest.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
using System;
2-
using System.Diagnostics.CodeAnalysis;
3-
2+
using System.Collections.Generic;
43
using Bunit.Mocking.JSInterop;
54
using Bunit.Rendering;
65
using Bunit.TestAssets.SampleComponents;
7-
86
using Shouldly;
97

108
using Xunit;
119

10+
using static Bunit.ComponentParameterFactory;
11+
1212
namespace Bunit
1313
{
14-
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "<Pending>")]
15-
[Obsolete("To be removed when ComponentTestFixture is removed")]
16-
public class ComponentTestFixtureTest : ComponentTestFixture
14+
public class ComponentParameterFactoryTest : TestContext
1715
{
1816
[Fact(DisplayName = "All types of parameters are correctly assigned to component on render")]
19-
public void Test001()
17+
public void Test005()
2018
{
2119
Services.AddMockJsRuntime();
2220

@@ -85,18 +83,6 @@ public void Test002()
8583
Should.Throw<Exception>(() => instance.ItemTemplate!("")(null)).Message.ShouldBe("ItemTemplate");
8684
}
8785

88-
[Fact(DisplayName = "Trying to set CascadingValue during SetParametersAndRender throws")]
89-
public void Test003()
90-
{
91-
// arrange
92-
Services.AddMockJsRuntime();
93-
var cut = RenderComponent<AllTypesOfParams<string>>();
94-
95-
// assert
96-
Should.Throw<InvalidOperationException>(() => cut.SetParametersAndRender(CascadingValue(42)));
97-
Should.Throw<InvalidOperationException>(() => cut.SetParametersAndRender(CascadingValue(nameof(AllTypesOfParams<string>.NamedCascadingValue), 1337)));
98-
}
99-
10086
[Fact(DisplayName = "Template(name, markupFactory) helper correctly renders markup template")]
10187
public void Test100()
10288
{

src/bunit.core.tests/Extensions/WaitForHelpers/RenderedFragmentWaitForHelperExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Test110()
3636
);
3737
}
3838

39-
[Fact(DisplayName = "WaitForAssertion throws exception after timeout")]
39+
[Fact(DisplayName = "WaitForAssertion throws assertion exception after timeout")]
4040
public void Test011()
4141
{
4242
var cut = RenderComponent<Simple1>();

src/bunit.core.tests/GlobalSuppressions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@
77

88
[assembly: SuppressMessage("Usage", "BL0006:Do not use RenderTree types")]
99
[assembly: SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "not in tests")]
10+
[assembly: SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "<Pending>")]
11+
[assembly: SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "<Pending>")]
12+
[assembly: SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "<Pending>")]
13+
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "In tests its ok to catch the general exception type")]
14+
[assembly: SuppressMessage("Usage", "CA2234:Pass system uri objects instead of strings", Justification = "<Pending>")]
15+
[assembly: SuppressMessage("Usage", "BL0006:Do not use RenderTree types", Justification = "<Pending>")]
16+
[assembly: SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores")]
17+
[assembly: SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes")]
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

src/bunit.core.tests/Rendering/ComponentParameterTest.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
3-
4-
using Bunit.Rendering;
5-
63
using Shouldly;
7-
84
using Xunit;
95

10-
namespace Bunit
6+
namespace Bunit.Rendering
117
{
128
public class ComponentParameterTest
13-
{
9+
{
1410
public static IEnumerable<object[]> GetEqualsTestData()
1511
{
1612
var name = "foo";
@@ -69,5 +65,5 @@ public void Test005(ComponentParameter left, ComponentParameter right, bool expe
6965
{
7066
left.GetHashCode().Equals(right.GetHashCode()).ShouldBe(expectedResult);
7167
}
72-
}
68+
}
7369
}

src/bunit.core/Extensions/LoggerHelperExtensions.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,5 @@ public static ILogger<TCategoryName> CreateLogger<TCategoryName>(this IServicePr
2222
var loggerFactory = services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance;
2323
return loggerFactory.CreateLogger<TCategoryName>();
2424
}
25-
26-
/// <summary>
27-
/// Creates a logger from the <see cref="ILoggerFactory"/> registered in the <see cref="IServiceProvider"/>.
28-
/// </summary>
29-
/// <param name="services">The service to get the <see cref="ILoggerFactory"/> from.</param>
30-
/// <param name="categoryName">The category for the logger.</param>
31-
/// <returns>The <see cref="ILogger"/></returns>
32-
public static ILogger CreateLogger(this IServiceProvider services, string categoryName)
33-
{
34-
var loggerFactory = services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance;
35-
return loggerFactory.CreateLogger(categoryName);
36-
}
3725
}
3826
}

src/bunit.core/Extensions/TimeSpanExtensions.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/bunit.core/RazorTesting/ComponentUnderTest.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ namespace Bunit
1313
public class ComponentUnderTest : FragmentBase
1414
{
1515
/// <inheritdoc />
16-
public override Task SetParametersAsync(ParameterView parameters)
17-
{
18-
var result = base.SetParametersAsync(parameters);
19-
return result;
20-
}
16+
public override Task SetParametersAsync(ParameterView parameters) => base.SetParametersAsync(parameters);
2117
}
2218
}

src/bunit.core/RazorTesting/FixtureBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ public override Task SetParametersAsync(ParameterView parameters)
8181
return base.SetParametersAsync(parameters);
8282
}
8383

84+
8485
/// <inheritdoc/>
86+
[SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "Validating component parameters")]
8587
public override void Validate()
8688
{
8789
base.Validate();
8890
if (ChildContent is null)
89-
throw new ArgumentException($"No '{nameof(ChildContent)}' specified in the {GetType().Name} component.");
91+
throw new ArgumentException($"No '{nameof(ChildContent)}' specified in the {GetType().Name} component.", nameof(ChildContent));
9092
if (Test is null && TestAsync is null)
91-
throw new ArgumentException($"No test action provided via the '{nameof(Test)}' or '{nameof(TestAsync)}' parameters to the {GetType().Name} component.");
93+
throw new ArgumentException($"No test action provided via the '{nameof(Test)}' or '{nameof(TestAsync)}' parameters to the {GetType().Name} component.", nameof(Test));
9294
if (Test is { } && TestAsync is { })
93-
throw new ArgumentException($"Only one of the '{nameof(Test)}' or '{nameof(TestAsync)}' actions can be provided to the {GetType().Name} component at the same time.");
95+
throw new ArgumentException($"Only one of the '{nameof(Test)}' or '{nameof(TestAsync)}' actions can be provided to the {GetType().Name} component at the same time.", nameof(Test));
9496
#pragma warning disable CS0618 // Type or member is obsolete
9597
if (Tests is { })
9698
throw new ArgumentException($"The use of the '{nameof(Tests)}' parameter has been obsoleted, and any methods assigned to it will not longer be invoked.");

src/bunit.core/RazorTesting/FragmentBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public virtual Task SetParametersAsync(ParameterView parameters)
2424
parameters.SetParameterProperties(this);
2525
if (ChildContent is null)
2626
throw new InvalidOperationException($"No {nameof(ChildContent)} specified in test component.");
27+
2728
return Task.CompletedTask;
2829
}
2930
}

0 commit comments

Comments
 (0)