Пример #1
0
        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 <ScoreContext>()
                              .UseSqlite(connection)
                              .Options;

                // Create the schema in the database
                using (var context = new ScoreContext(options))
                {
                    context.Database.EnsureCreated();
                }

                // Run the test against one instance of the context
                using (var context = new ScoreContext(options))
                {
                    var service = new ScoreBoardService(context);
                    service.Add("Shewawa");
                    context.SaveChanges();
                }

                // Use a separate instance of the context to verify correct data was saved to database
                using (var context = new ScoreContext(options))
                {
                    Assert.Equal(1, context.Scores.Count());
                    Assert.Equal("Shewawa", context.Scores.Single().Name);
                }
            }
            finally
            {
                connection.Close();
            }
        }