Outils pour utilisateurs

Outils du site


developpement:dotnet:moq:toc

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
developpement:dotnet:moq:toc [2018/01/14 22:38] – [Setup des méthodes mockées] sgariepydeveloppement:dotnet:moq:toc [2022/02/02 00:42] (Version actuelle) – modification externe 127.0.0.1
Ligne 3: Ligne 3:
 Pour une utilisation en .NET Core 2.x de NUNit, voir [[https://github.com/nunit/docs/wiki/.NET-Core-and-.NET-Standard|cette documentation]]. Pour une utilisation en .NET Core 2.x de NUNit, voir [[https://github.com/nunit/docs/wiki/.NET-Core-and-.NET-Standard|cette documentation]].
  
 +Nuget Packages:
 +  * NUnit
 +  * NUnit3TestAdapter
 +  * Microsoft.NET.Test.Sdk
  
  
  
 ====== Moq ====== ====== Moq ======
 +
 +Nuget Package: Moq
  
 Un **Test Double** est un terme générique pour désigner un élément de production dans l'objectif de tester((https://martinfowler.com/bliki/TestDouble.html)) Un **Test Double** est un terme générique pour désigner un élément de production dans l'objectif de tester((https://martinfowler.com/bliki/TestDouble.html))
Ligne 26: Ligne 32:
 var sut = new CreditCardApplication(mockValidator.Object); var sut = new CreditCardApplication(mockValidator.Object);
 </code> </code>
 +
 +Pour mocker, on doit avoir une interface, une classe abstraite, ou une class non-sealed.
  
 ====== Setup des méthodes mockées ====== ====== Setup des méthodes mockées ======
Ligne 46: Ligne 54:
   mockValidator.Setup(x => x.IsValid(It.IsIn("x", "y", "z"))).Returns(true);   mockValidator.Setup(x => x.IsValid(It.IsIn("x", "y", "z"))).Returns(true);
   mockValidator.Setup(x => x.IsValid(It.IsInRange("b", "z", Range.Inclusive))).Returns(true);   mockValidator.Setup(x => x.IsValid(It.IsInRange("b", "z", Range.Inclusive))).Returns(true);
-====== MockBehavior ======+ 
 + 
 +===== Out parameter ===== 
 + 
 + 
 +  bool isValid = true; 
 +  mockValidator.Setup(x => x.IsValid(It.IsAny<string>(), out isValid)); 
 + 
 + 
 +====== Mocker les propriétés ====== 
 + 
 + 
 +  mockValidator.Setup(x => x.LicenseKey).Returns("EXPIRED"); 
 + 
 + 
 +===== Par une fonction ===== 
 + 
 + 
 + 
 + 
 +  mockValidator.Setup(x => x.LicenseKey).Returns(GetLicenseKeyExpiryString); 
 + 
 + 
 +  string GetLicenseKeyExpiryString() 
 +  { 
 +      return "EXPIRED"; 
 +  } 
 + 
 +===== Propriétés imbriquées ===== 
 + 
 +Si on a cette définition de ''IFrequentFlyerNumberValidator'': 
 + 
 +<code csharp> 
 +namespace CreditCardApplications 
 +
 +    public interface ILicenseData 
 +    { 
 +        string LicenseKey { get; } 
 +    } 
 + 
 +    public interface IServiceInformation 
 +    { 
 +        ILicenseData License { get; set; } 
 +    } 
 + 
 +    public interface IFrequentFlyerNumberValidator 
 +    { 
 +        bool IsValid(string frequentFlyerNumber); 
 +        void IsValid(string frequentFlyerNumber, out bool isValid); 
 +        IServiceInformation ServiceInformation { get; } 
 +    } 
 +
 +</code> 
 + 
 +On peut faire ceci : 
 + 
 +<code csharp> 
 +var mockValidator = new Mock<IFrequentFlyerNumberValidator>(); 
 + 
 +var mockLicenseData = new Mock<ILicenseData>(); 
 +mockLicenseData.Setup(x => x.LicenseKey).Returns("EXPIRED"); 
 + 
 +var mockServiceInfo = new Mock<IServiceInformation>(); 
 +mockServiceInfo.Setup(x => x.License).Returns(mockLicenseData.Object); 
 + 
 +mockValidator.Setup(x => x.ServiceInformation).Returns(mockServiceInfo.Object); 
 +</code> 
 + 
 +ou simplement : 
 + 
 +  mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns("EXPIRED"); 
 + 
 + 
 + 
 +===== Valeurs de retour par défaut ===== 
 + 
 +  * Reference types : null 
 +  *  
 + 
 + 
 + 
 +  mockValidator.DefaultValue = Default.Mock; 
 + 
 + 
 +===== Track changes to mock property values ===== 
 + 
 + 
 +Par défaut, Moq ne retient pas les changements aux propriétés mockées. 
 + 
 +  _validator.ValidationMode = application.Age >= 30 ? ValidationMode.Detailed :  
 +                                                      ValidationMode.Quick; 
 + 
 +On doit donc faire un setup de propriété: 
 + 
 +  mockValidator.SetupProperty(x => x.ValidationMode); 
 + 
 + 
 +Pour toutes les propriétés: 
 + 
 +  mockValidator.SetupAllProperties(); 
 + 
 +====== Verify ====== 
 + 
 + 
 +===== MockBehavior =====
  
 Il y a deux modes de ''MockBehavior'' : ''Strict'' et ''Loose'' : Il y a deux modes de ''MockBehavior'' : ''Strict'' et ''Loose'' :
Ligne 56: Ligne 168:
 On suggère d'utiliser des mocks stricts seulement quand c'est nécessaire et préférer des mocks loose dans les autres cas. On suggère d'utiliser des mocks stricts seulement quand c'est nécessaire et préférer des mocks loose dans les autres cas.
  
 +
 +
 +
 +====== FluentAssertions ======
 +
 +Nuget Package : FluentAssertions.
 +
 +
 +Exemple:
 +
 +
 +  decision.Should().Be(CreditCardApplicationDecision.AutoAccepted);
  
  
developpement/dotnet/moq/toc.1515965927.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)