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:54] sgariepydeveloppement:dotnet:moq:toc [2022/02/02 00:42] (Version actuelle) – modification externe 127.0.0.1
Ligne 32: 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 79: Ligne 81:
       return "EXPIRED";       return "EXPIRED";
   }   }
-====== MockBehavior ======+ 
 +===== 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'' :
developpement/dotnet/moq/toc.1515966865.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)