-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from Blazored/hexabit-main
Adding bUnit tests
- Loading branch information
Showing
27 changed files
with
1,105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/Blazored.FluentValidation.Tests/AssemblyScanning/Component.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<EditForm | ||
Model="@_person" | ||
OnValidSubmit="ValidSubmit" | ||
OnInvalidSubmit="InvalidSubmit"> | ||
|
||
@if (DisableAssemblyScanning is null) | ||
{ | ||
<FluentValidationValidator /> | ||
} | ||
else | ||
{ | ||
<FluentValidationValidator DisableAssemblyScanning="DisableAssemblyScanning.Value" /> | ||
} | ||
|
||
<ValidationSummary/> | ||
|
||
<p> | ||
<label>First name: </label> | ||
<InputText name="@nameof(_person.FirstName)" @bind-Value="@_person.FirstName"/> | ||
</p> | ||
|
||
<button type="submit">Save</button> | ||
</EditForm> | ||
|
||
@code { | ||
[Parameter] public bool? DisableAssemblyScanning { get; set; } | ||
private readonly Person _person = new(); | ||
|
||
internal ValidationResultType Result { get; private set; } = ValidationResultType.Valid; | ||
|
||
private void ValidSubmit() => Result = ValidationResultType.Valid; | ||
private void InvalidSubmit() => Result = ValidationResultType.Error; | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Blazored.FluentValidation.Tests/AssemblyScanning/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
### What does this test? | ||
This test checks if the assembly scanning works. It leverages, that this test | ||
assembly does not register any `AbstractValidator` by default. | ||
|
||
- Setting the `DisableAssemblyScanning` to `true` should not find any validators and ignore errors. | ||
- Setting the `DisableAssemblyScanning` to `false` or not setting the attribute at all, should | ||
find the validators in the assembly and validate normally. | ||
- Setting the `DisableAssemblyScanning` to `true` and registering the validators manually should | ||
find the validators and validate normally. |
80 changes: 80 additions & 0 deletions
80
tests/Blazored.FluentValidation.Tests/AssemblyScanning/Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using Blazored.FluentValidation.Tests.Model; | ||
|
||
namespace Blazored.FluentValidation.Tests.AssemblyScanning; | ||
|
||
public class Tests : TestContext | ||
{ | ||
private readonly Fixture _fixture = new(); | ||
|
||
[Fact] | ||
public void DisableAssemblyScanning_SetToTrue_NoValidationHappens() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(p => p.Add(c => c.DisableAssemblyScanning, true)); | ||
var person = _fixture.InvalidPerson(); | ||
|
||
// Act | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Valid); | ||
} | ||
|
||
[Fact] | ||
public void DisableAssemblyScanning_SetToFalse_ValidationHappens() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(p => p.Add(c => c.DisableAssemblyScanning, false)); | ||
var person = _fixture.InvalidPerson(); | ||
|
||
// Act | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
[Fact] | ||
public void DisableAssemblyScanning_NotSet_ValidationHappens() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(p => p.Add(c => c.DisableAssemblyScanning, null)); | ||
var person = _fixture.InvalidPerson(); | ||
|
||
// Act | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
[Fact] | ||
public void DisableAssemblyScanning_SetToTrueButValidatorsRegistered_ValidationHappens() | ||
{ | ||
// Arrange | ||
Services.AddTransient<AbstractValidator<Person>, PersonValidator>(); | ||
var cut = RenderComponent<Component>(p => p.Add(c => c.DisableAssemblyScanning, null)); | ||
var person = _fixture.InvalidPerson(); | ||
|
||
// Act | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
private class Fixture | ||
{ | ||
public Person InvalidPerson() => new() | ||
{ | ||
FirstName = "", | ||
LastName = "Doe", | ||
EmailAddress = "[email protected]", | ||
Age = 30 | ||
}; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/Blazored.FluentValidation.Tests/BasicValidation/Component.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<EditForm | ||
Model="@_person" | ||
OnValidSubmit="ValidSubmit" | ||
OnInvalidSubmit="InvalidSubmit"> | ||
|
||
<FluentValidationValidator/> | ||
<ValidationSummary/> | ||
|
||
<p> | ||
<label>First name: </label> | ||
<InputText name="@nameof(_person.FirstName)" @bind-Value="@_person.FirstName"/> | ||
</p> | ||
|
||
<p> | ||
<label>Last name: </label> | ||
<InputText name="@nameof(_person.LastName)" @bind-Value="@_person.LastName"/> | ||
</p> | ||
|
||
<p> | ||
<label>Age: </label> | ||
<InputNumber name="@nameof(_person.Age)" @bind-Value="@_person.Age"/> | ||
</p> | ||
|
||
<p> | ||
<label>Email Address: </label> | ||
<InputText name="@nameof(_person.EmailAddress)" @bind-Value="@_person.EmailAddress"/> | ||
</p> | ||
|
||
<p> | ||
<label>Address Line 1: </label> | ||
<InputText name="@nameof(_person.Address.Line1)" @bind-Value="@_person.Address!.Line1"/> | ||
</p> | ||
|
||
<button type="submit">Save</button> | ||
</EditForm> | ||
|
||
@code { | ||
private readonly Person _person = new() { Address = new() }; | ||
internal ValidationResultType Result { get; private set; } = ValidationResultType.Valid; | ||
|
||
private void ValidSubmit() => Result = ValidationResultType.Valid; | ||
private void InvalidSubmit() => Result = ValidationResultType.Error; | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Blazored.FluentValidation.Tests/BasicValidation/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### What does this test? | ||
This test checks if the basic validation works. | ||
|
||
- Does a valid model pass the validation? | ||
- Do basic validation rules get picked up? | ||
- Do validation errors get displayed correctly in the UI? | ||
- Are nested rules validated correctly? |
110 changes: 110 additions & 0 deletions
110
tests/Blazored.FluentValidation.Tests/BasicValidation/Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Blazored.FluentValidation.Tests.Model; | ||
|
||
namespace Blazored.FluentValidation.Tests.BasicValidation; | ||
|
||
public class Tests : TestContext | ||
{ | ||
private readonly Fixture _fixture = new(); | ||
|
||
[Fact] | ||
public void Validate_DataIsValid_ValidSubmit() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson(); | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Valid); | ||
} | ||
|
||
[Fact] | ||
public void Validate_FirstNameMissing_InvalidSubmit() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { FirstName = string.Empty }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Instance.Result.Should().Be(ValidationResultType.Error); | ||
} | ||
|
||
[Fact] | ||
public void Validate_FirstNameMissing_ValidationErrorsPresent() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { FirstName = string.Empty }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Find(".validation-errors>.validation-message").TextContent.Should().Contain(PersonValidator.FirstNameRequired); | ||
cut.Find("li.validation-message").TextContent.Should().Contain(PersonValidator.FirstNameRequired); | ||
} | ||
|
||
[Fact] | ||
public void Validate_AgeTooOld_ValidationErrorsPresent() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { Age = 250 }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Find(".validation-errors>.validation-message").TextContent.Should().Contain(PersonValidator.AgeMax); | ||
} | ||
|
||
[Fact] | ||
public void Validate_AddressLine1Missing_ValidationErrorsPresent() | ||
{ | ||
// Arrange | ||
var cut = RenderComponent<Component>(); | ||
var person = _fixture.ValidPerson() with { Address = new() { Line1 = string.Empty } }; | ||
|
||
// Act | ||
FillForm(cut, person); | ||
cut.Find("button").Click(); | ||
|
||
// Assert | ||
cut.Find(".validation-errors>.validation-message").TextContent.Should().Contain(AddressValidator.Line1Required); | ||
} | ||
|
||
private static void FillForm(IRenderedComponent<Component> cut, Person person) | ||
{ | ||
cut.Find($"input[name={nameof(Person.FirstName)}]").Change(person.FirstName); | ||
cut.Find($"input[name={nameof(Person.LastName)}]").Change(person.LastName); | ||
cut.Find($"input[name={nameof(Person.EmailAddress)}]").Change(person.EmailAddress); | ||
cut.Find($"input[name={nameof(Person.Age)}]").Change(person.Age.ToString()); | ||
cut.Find($"input[name={nameof(Person.Address.Line1)}]").Change(person.Address!.Line1); | ||
} | ||
|
||
private class Fixture | ||
{ | ||
public Person ValidPerson() => new() | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
EmailAddress = "[email protected]", | ||
Age = 30, | ||
Address = new() | ||
{ | ||
Line1 = "123 Main St", | ||
Town = "Springfield", | ||
Postcode = "12345" | ||
} | ||
}; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Blazored.FluentValidation.Tests/Blazored.FluentValidation.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Bunit" /> | ||
<Using Include="Bunit.TestDoubles" /> | ||
<Using Include="Microsoft.Extensions.DependencyInjection" /> | ||
<Using Include="Xunit"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="bunit" Version="1.26.64" /> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="FluentValidation" Version="11.9.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="xunit" Version="2.6.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Blazored.FluentValidation\Blazored.FluentValidation.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
|
||
</Project> |
Oops, something went wrong.