HelpersUnit 1.0.0
dotnet add package HelpersUnit --version 1.0.0
NuGet\Install-Package HelpersUnit -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HelpersUnit" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HelpersUnit --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HelpersUnit, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HelpersUnit as a Cake Addin #addin nuget:?package=HelpersUnit&version=1.0.0 // Install HelpersUnit as a Cake Tool #tool nuget:?package=HelpersUnit&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HelpersUnit
Il a pour but d'aider dans la création des tests unitaires. C'est un ensemble d'outils (helpers, extensions,...).
Docs - Helpers
MoqHelper
La class MoqHelper
est une aide pour la création de Mock.
public IEnumerable<Mock> CreateMockParamsConstructor<T>(params Type[] constructorTypes)
Crée les Mock des paramètres du constructeur du type passé. Si la listeparams
est vide, il prendra le premier constructeur qu'il trouve.
Pour selectionner le constructeur (si plusieurs), il faut passer la liste de type qui sont présent dans ce constructeur.
Ensuite, il suffit de récupérer le Mock voulu pour lui ajouter des comportements.
Exemple complet :
#region Arrange
MoqHelper moqHelper = new MoqHelper();
// Permet de créer une liste de mock de chaque paramètre du constructeur.
// la class UnSuperController a 24 paramètres.
IEnumerable<Mock> allMockParams = moqHelper.CreateMockParamsConstructor<UnSuperController>();
// Méthode d'extension pour récupérer un Mock particulier de la liste.
Mock<IWebDetailsService> mockOffre = allMockParams.GetMock<IWebDetailsService>();
mockOffre.Setup(x => x.GetDetailViewModel(123, 666, false, false))
.Returns(new DetailViewModel());
// même code, mais avec une écriture "fluide"
allMockParams.GetMock<IWebDetailsService>()
.Setup(x => x.GetDetailViewModel(123, 666, false, false))
.Returns(new OffreViewModel());
#endregion
ObjectHelpers
La class ObjectHelpers
permet de faire des actions sur un objet/instance.
public static void ReplaceFieldValue<T>(T obj, string namePrivateField, object newValue)
Permet de modifier la valeur d'un champ privé (field) par une nouvelle valeur.
// ...methode de test...
#region Arrange
// [..du code..]
Mock<IAuthService> mockAuth = new Mock<IAuthService>();
mockAuth.Setup(x => x.GetEmailFailedHistory(It.IsAny<string>()))
.Returns(new Common.Result.InfoResult<int>() { Result = 2 });
mockAuth.Setup(x => x.GetByEmail(It.IsAny<string>()))
.Returns(new InfoResult<AuthUser>()
{
Success = false
});
#endregion
#region Act
ResourceOwnerPasswordValidator resourceOwner = new ResourceOwnerPasswordValidator(config, mockLogger.Object);
// Remplace le field "_authManager" par notre Mock.
ObjectHelpers.ReplaceFieldValue<ResourceOwnerPasswordValidator>(resourceOwner, "_authManager", mockAuth.Object);
var result = resourceOwner.ValidateAsync(context);
#endregion
ObjectFillerHelper
La class ObjectFillerHelper
permet de faire de "nourrir" un objet, via des paramètres.
public static T FillConstructorOf<T>(IEnumerable<Mock> mockList)
En passant une liste de Mock, cette méthode va construire un objet.
[Test]
public async Task UserBlocked_Then_NoLogin()
{
#region Arrange
MoqHelper moqHelper = new MoqHelper();
var paramsMock = moqHelper.CreateMockParamsConstructor<CompteController>();
// [... code pour mock les comportements...]
#endregion
#region Act
CompteController controller = ObjectFillerHelper.FillConstructorOf<CompteController>(paramsMock);
#endregion
Generate
La class Generate
permet de générer du contenu (string, email) :
public static string GenerateString(int longueur)
Permet de créer un string aléatoirement avec une longueur spécifiée.
string random = Generate.GenerateString(12);
// output : "AsIU1g4Vcp4";
public static string GenerateString(int longueur, string caractresUtilisatble)
Permet de créer un string aléatoirement avec une longueur spécifiée et les caractères voulus.
string random = Generate.GenerateString(5, "ABCDefgh");
// output : "AeCfg";
public static string GenerateRandomEmail()
Permet de générer un mail par rapport aux constantes de la lib.
string email = Generate.GenerateRandomEmail();
// output : "Kai-Haley866@yahoo.com
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Moq (>= 4.18.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 157 | 9/1/2023 |