public Task <Unit> Handle(SaveLookUpHistoryCommand command, CancellationToken cancellationToken) { var newEntry = new LookUpHistory { Word = command.Word, Definition = command.Definition, User = command.User }; _db.LookUpHistories.Add(newEntry); _db.SaveChanges(); return(Task.FromResult(Unit.Value)); }
public void Add_writes_to_database() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var options = new DbContextOptionsBuilder <AppDbContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new AppDbContext(options)) { context.Database.EnsureCreated(); } // Run the test against one instance of the context using (var context = new AppDbContext(options)) { var history = new LookUpHistory { Word = "ace", Definition = "top card", User = new AppUser() }; context.LookUpHistories.Add(history); context.SaveChanges(); } // Use a separate instance of the context to verify correct data was saved to database using (var context = new AppDbContext(options)) { context.LookUpHistories.Count().ShouldBe(1); } } finally { connection.Close(); } }