From ffd725f1542c236ba48370d5a1b59cac39cc8f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Chevaldonn=C3=A9?= Date: Tue, 31 Dec 2019 23:51:28 +0100 Subject: [PATCH] updated sample about InMemory --- .../ex_041_004_InMemory/ReadMe.md | 45 ++++++++++++++++++- .../ex_041_004_UnitTests/NounoursDB_Tests.cs | 1 - 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/p08_BDD_EntityFramework/ex_041_004_InMemory/ReadMe.md b/p08_BDD_EntityFramework/ex_041_004_InMemory/ReadMe.md index 9ab4d4b..44d2ed3 100644 --- a/p08_BDD_EntityFramework/ex_041_004_InMemory/ReadMe.md +++ b/p08_BDD_EntityFramework/ex_041_004_InMemory/ReadMe.md @@ -65,7 +65,50 @@ La classe ```NounoursContext``` peut donc s'utiliser comme précédemment, sans ## Configuration du test unitaire Le test unitaire est de type *xUnit* et va permettre d'injecter le fournisseur **InMemory**. - +* On crée un nouveau projet de tests unitaires (*xUnit*) +* On lui ajoute le package NuGet : *Microsoft.EntityFrameworkCore.InMemory* +* On ajoute également une référence au projet précédent (*ex_041_004_InMemory.exe*) +* On peut ensuite écrire un premier test comme suit : +```csharp +using ex_041_004_InMemory; +using Microsoft.EntityFrameworkCore; +using System.Linq; +using Xunit; + +namespace ex_041_004_UnitTests +{ + public class NounoursDB_Tests + { + [Fact] + public void Add_Test() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Add_writes_to_database") + .Options; + + // runs the test against one instance of the context + using (var context = new NounoursContext(options)) + { + Nounours chewie = new Nounours { Nom = "Chewbacca" }; + Nounours yoda = new Nounours { Nom = "Yoda" }; + Nounours ewok = new Nounours { Nom = "Ewok" }; + + context.Nounours.Add(chewie); + context.Nounours.Add(yoda); + context.Nounours.Add(ewok); + context.SaveChanges(); + } + + // Use a separate instance of the context to verify correct data was saved to database + using (var context = new NounoursContext(options)) + { + Assert.Equal(3, context.Nounours.Count()); + Assert.Equal("Chewbacca", context.Nounours.First().Nom); + } + } + } +} +``` test * nouveau projet diff --git a/p08_BDD_EntityFramework/ex_041_004_UnitTests/NounoursDB_Tests.cs b/p08_BDD_EntityFramework/ex_041_004_UnitTests/NounoursDB_Tests.cs index 8fe34da..e91d1da 100644 --- a/p08_BDD_EntityFramework/ex_041_004_UnitTests/NounoursDB_Tests.cs +++ b/p08_BDD_EntityFramework/ex_041_004_UnitTests/NounoursDB_Tests.cs @@ -1,6 +1,5 @@ using ex_041_004_InMemory; using Microsoft.EntityFrameworkCore; -using System; using System.Linq; using Xunit;