diff --git a/Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs b/Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs new file mode 100644 index 0000000..7525335 --- /dev/null +++ b/Tests/TicketManager.Tests/UnitTests/ControllersTests/ControllerTests.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading.Tasks; +using TicketManager.Data; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TicketManager._ +{ + public class ControllersTests + { + + public static void Wrapper( + Func, Task> Test, + Action> SeedDb) + { + // Create inMemory Test Database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + try + { + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + // creates DB schema + using (var context = new AppDbContext(options)) + { + context.Database.EnsureCreated(); + } + + // Seed DB usng one context instance + SeedDb(options); + + // use another context instance to run the test + Test(options); + } + finally + { + connection.Close(); + } + } + } +} \ No newline at end of file diff --git a/Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs b/Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs new file mode 100644 index 0000000..1118c8a --- /dev/null +++ b/Tests/TicketManager.Tests/UnitTests/ControllersTests/SeedDb.cs @@ -0,0 +1,34 @@ +using System; +using TicketManager.Data; +using TicketManager.Models; +using Microsoft.EntityFrameworkCore; + +namespace TicketManager._ +{ + public class SeedDb + { + public static void Projects(DbContextOptions options) + // Seed DB usng one context instance + { + using (var context = new AppDbContext(options)) + { + context.Projects.AddRange( + new Project() + { + Id = 1, + Title = "Secret Project", + Description = "Shht Don't Ask don't tell", + PlannedEnding = new DateTime(2021, 7, 21) + }, + new Project() + { + Id = 2, + Title = "Public Project", + Description = "It's quite obvious, isn't it?!", + PlannedEnding = new DateTime(2036, 6, 16) + }); + context.SaveChanges(); + } + } + } +} \ No newline at end of file