using DbContextLib; using Entities; using Microsoft.Extensions.Options; using Microsoft.EntityFrameworkCore; using System.Linq; using Xunit; using Microsoft.Data.Sqlite; namespace UnitTests { public class UnitTest1 { [Fact] public void Add_Test() { //connection must be opened to use In-memory database var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder.UseSqlLite().options; //prepares the database with one instance of the context using (var context = new LibraryContext(options)) { //context.Database.OpenConnection(); context.Database.EnsureCreated(); BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "" }; BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" ); BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" ); context.BooksSet.Add(the100); context.BooksSet.Add(princeOfPersia); context.BooksSet.Add(PercyJackson); context.SaveChanges(); } //uses another instance of the context to do the tests using (var context = new LibraryContext(options)) { context.Database.EnsureCreated(); Assert.Equal(3, context.BooksSet.Count()); Assert.Equal("the100", context.BooksSet.First().Title); } } } }