developpement:dotnet:moq:toc
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteDernière révisionLes deux révisions suivantes | ||
developpement:dotnet:moq:toc [2018/01/14 22:40] – [Setup des méthodes mockées] sgariepy | developpement:dotnet:moq:toc [2018/01/14 23:59] – [MockBehavior] sgariepy | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
Pour une utilisation en .NET Core 2.x de NUNit, voir [[https:// | Pour une utilisation en .NET Core 2.x de NUNit, voir [[https:// | ||
+ | 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' | Un **Test Double** est un terme générique pour désigner un élément de production dans l' | ||
Ligne 26: | Ligne 32: | ||
var sut = new CreditCardApplication(mockValidator.Object); | var sut = new CreditCardApplication(mockValidator.Object); | ||
</ | </ | ||
+ | |||
+ | 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 55: | Ligne 63: | ||
+ | ====== Mocker les propriétés ====== | ||
+ | mockValidator.Setup(x => x.LicenseKey).Returns(" | ||
- | ====== | + | ===== Par une fonction |
+ | |||
+ | |||
+ | |||
+ | |||
+ | mockValidator.Setup(x => x.LicenseKey).Returns(GetLicenseKeyExpiryString); | ||
+ | |||
+ | |||
+ | string GetLicenseKeyExpiryString() | ||
+ | { | ||
+ | return " | ||
+ | } | ||
+ | |||
+ | ===== Propriétés imbriquées ===== | ||
+ | |||
+ | Si on a cette définition de '' | ||
+ | |||
+ | <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, | ||
+ | IServiceInformation ServiceInformation { get; } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | On peut faire ceci : | ||
+ | |||
+ | <code csharp> | ||
+ | var mockValidator = new Mock< | ||
+ | |||
+ | var mockLicenseData = new Mock< | ||
+ | mockLicenseData.Setup(x => x.LicenseKey).Returns(" | ||
+ | |||
+ | var mockServiceInfo = new Mock< | ||
+ | mockServiceInfo.Setup(x => x.License).Returns(mockLicenseData.Object); | ||
+ | |||
+ | mockValidator.Setup(x => x.ServiceInformation).Returns(mockServiceInfo.Object); | ||
+ | </ | ||
+ | |||
+ | ou simplement : | ||
+ | |||
+ | mockValidator.Setup(x => x.ServiceInformation.License.LicenseKey).Returns(" | ||
+ | |||
+ | |||
+ | |||
+ | ===== 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 '' | Il y a deux modes de '' | ||
Ligne 69: | Ligne 168: | ||
On suggère d' | On suggère d' | ||
+ | |||
+ | |||
+ | |||
+ | ====== FluentAssertions ====== | ||
+ | |||
+ | Nuget Package : FluentAssertions. | ||
+ | |||
+ | |||
+ | Exemple: | ||
+ | |||
+ | |||
+ | decision.Should().Be(CreditCardApplicationDecision.AutoAccepted); | ||
developpement/dotnet/moq/toc.txt · Dernière modification : 2022/02/02 00:42 de 127.0.0.1