updated sample about InMemory

EFCore3_Reforged
Marc CHEVALDONNE 6 years ago
parent 11dda360a9
commit ffd725f154

@ -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<NounoursContext>()
.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

@ -1,6 +1,5 @@
using ex_041_004_InMemory;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using Xunit;

Loading…
Cancel
Save