public void UnitOfWorkDoNotShowAsExisitngWithoutCommit(Company input) { // Arrange var testSettings = new TestProjectSettings(); DbContext testContext = null; StockUnitOfWork tested = null; try { testContext = new StockContext(testSettings); testContext.Database.EnsureCreated(); tested = new StockUnitOfWork(testContext); // Act tested.StockRepository.Add(input); var expected = 0; var actual = tested.StockRepository.GetAll().ToList().Count; // Assert Assert.Equal(expected, actual); } finally { testContext?.Database.EnsureDeleted(); tested?.Dispose(); testContext?.Dispose(); } }
public void FactoryProvidesNewInstanceEverytimeItsCalled() { // Arrange var testSettings = new TestProjectSettings(); StockContext testContext = null; StockUnitOfWorkFactory ufo = null; StockUnitOfWork uow = null; StockUnitOfWork uow2 = null; try { testContext = new StockContext(testSettings); ufo = new StockUnitOfWorkFactory(testContext); // Act uow = ufo.GetInstance(); uow2 = ufo.GetInstance(); // Assert Assert.NotEqual(uow, uow2); } finally { testContext?.Dispose(); ufo?.Dispose(); uow?.Dispose(); } }
public void UnitOfWorkCommits(Company input) { // Arrange var testSettings = new TestProjectSettings(); DbContext testContext = null; StockUnitOfWork tested = null; try { testContext = new StockContext(testSettings); testContext.Database.EnsureCreated(); tested = new StockUnitOfWork(testContext); // Act tested.StockRepository.Add(input); tested.Complete(); var expected = 1; var actual = tested.StockRepository.Count(); // Assert Assert.Equal(expected, actual); } finally { testContext?.Database.EnsureDeleted(); tested?.Dispose(); testContext?.Dispose(); } }
static async Task Main() { var dbName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); var proj = new ProjectSettings { ConnectionString = $"server=(localdb)\\MSSQLLocalDB;Initial Catalog={dbName};Integrated Security=True;" }; var input = GetStub(); DbContext context = null; StockUnitOfWork tested = null; try { var res = await DbManagementService.EnsureDbExists(proj); context = new StockContext(proj); tested = new StockUnitOfWork(context); tested.StockRepository.Add(input); tested.Complete(); var actual = tested.StockRepository.Count(); Console.WriteLine($"Added {actual} record(s) to db"); Console.ReadKey(); await DbManagementService.EnsureDbDoesNotExist(proj); } finally { tested?.Dispose(); if (context != null) { await context.DisposeAsync(); } } }