Skip to content

Commit

Permalink
Add TestsWithInMemoryDbBase abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
Zjyslav committed May 27, 2024
1 parent f75448c commit 0479ec4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@

namespace TutorLizard.BusinessLogic.Tests.Services.Browse;

public abstract class BrowseServiceTestsBase : IDisposable
public abstract class BrowseServiceTestsBase : TestsWithInMemoryDbBase
{
protected BrowseService BrowseService;
protected JaszczurContext DbContext;
protected Fixture Fixture = new();
protected Mock<IDbRepository<Ad>> MockAdRepository = new();
protected Mock<IDbRepository<ScheduleItem>> MockScheduleItemRepository = new();

public BrowseServiceTestsBase()
protected BrowseServiceTestsBase() : base()
{
BrowseService = new(MockAdRepository.Object, MockScheduleItemRepository.Object);
DbContext = SetupInMemoryDbContext();
}

public void Dispose()
{
DbContext.Dispose();
}

protected void SetupMockGetAllAds(List<Ad> ads)
Expand Down Expand Up @@ -138,12 +131,4 @@ protected IQueryable<TEntity> AddEntitiesToInMemoryDb<TEntity>(List<TEntity> ent
.Set<TEntity>()
.AsQueryable();
}

private JaszczurContext SetupInMemoryDbContext()
{
DbContextOptionsBuilder<JaszczurContext> dbBuilder = new();
dbBuilder.UseInMemoryDatabase(databaseName: $"FakeDb{Guid.NewGuid()}");
JaszczurContext context = new(dbBuilder.Options);
return context;
}
}
24 changes: 24 additions & 0 deletions Tests/TutorLizard.BusinessLogic.Tests/TestsWithInMemoryDbBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using TutorLizard.BusinessLogic.Data;

namespace TutorLizard.BusinessLogic.Tests;
public abstract class TestsWithInMemoryDbBase : IDisposable
{
protected JaszczurContext DbContext;

protected TestsWithInMemoryDbBase()
{
DbContext = SetupInMemoryDbContext();
}
public void Dispose()
{
DbContext.Dispose();
}
private JaszczurContext SetupInMemoryDbContext()
{
DbContextOptionsBuilder<JaszczurContext> dbBuilder = new();
dbBuilder.UseInMemoryDatabase(databaseName: $"FakeDb{Guid.NewGuid()}");
JaszczurContext context = new(dbBuilder.Options);
return context;
}
}

0 comments on commit 0479ec4

Please sign in to comment.