So, i'm learning .Net Core 3 and i have set up a web api with some actions, and i would like to test my repositories. Problem is, i'm using ef core with postgres(npgsql), and i'm using a generic repository pattern. I'm having trouble setting up the tests. I tried to set up an In Memory Database to replicate the context functionality, but, I can't get it to work.
Here's my BaseRepository:
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
protected AppDbContext _context;
public RepositoryBase(AppDbContext context)
{
_context = context;
}
public IQueryable<T> FindAll(bool trackChanges)
{
return !trackChanges ? _context.Set<T>().AsNoTracking() : _context.Set<T>();
}
public IQueryable<T> FindByCondition(Expression<Func<T, bool>> expression, bool trackChanges)
{
return !trackChanges
? _context.Set<T>().Where(expression).AsNoTracking()
: _context.Set<T>().Where(expression);
}
public void Create(T entity)
{
_context.Set<T>().Add(entity);
}
public void Update(T entity)
{
_context.Set<T>().Update(entity);
}
public void Delete(T entity)
{
_context.Set<T>().Remove(entity);
}
}
And here's my Tool Repository
public class ToolRepository : RepositoryBase<Tool>, IToolRepository
{
public ToolRepository(AppDbContext context) : base(context)
{
}
public async Task<PagedList<Tool>> GetAllToolsAsync(ToolParameters toolParameters, bool trackChanges)
{
List<Tool> tools;
if (toolParameters.Tag != null)
{
tools = await FindByCondition(tool => tool.Tags.Contains(toolParameters.Tag), trackChanges)
.OrderBy(tool => tool.Title)
.ToListAsync();
}
else
{
tools = await FindAll(trackChanges)
.OrderBy(tool => tool.Title)
.ToListAsync();
}
return PagedList<Tool>.ToPagedList(tools, toolParameters.PageNumber, toolParameters.PageSize);
}
public async Task<Tool> GetToolAsync(Guid toolId, bool trackChanges)
{
return await FindByCondition(tool => tool.Id.Equals(toolId), trackChanges).SingleOrDefaultAsync();
}
public void CreateTool(Tool tool)
{
Create(tool);
}
public void DeleteTool(Tool tool)
{
Delete(tool);
}
}
My Context with a dbSet
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ToolConfiguration());
}
public DbSet<Tool> Tools { get; set; }
}
And finally my test class:
public class ToolRepositoryTests
{
private readonly List<Tool> _tools;
private DbContextOptions<AppDbContext> options;
public ToolRepositoryTests()
{
options = new DbContextOptionsBuilder<AppDbContext>().UseInMemoryDatabase(databaseName: "VuttrDb").Options;
using (var context = new AppDbContext(options))
{
context.Tools.Add(new Tool
{
Id = new Guid("ab2bd817-98cd-4cf3-a80a-53ea0cd9c200"),
Title = "Postgre SQL",
Description = "Database Tool",
Link = "http://postgres.com",
Tags = new[]
{
"db",
"postgre",
"sql"
}
});
context.Tools.Add(new Tool
{
Id = new Guid("ab2bd817-98cd-4cf3-a80a-53ea0cd9c200"),
Title = "Insomnia",
Description = "Rest Client",
Link = "http://insomnia.com",
Tags = new[]
{
"http",
"api",
"rest"
}
});
context.Tools.Add(new Tool
{
Id = new Guid("ab2bd817-98cd-4cf3-a80a-53ea0cd9c200"),
Title = "Rider",
Link = "https://www.jetbrains.com/pt-br/rider/",
Description = "C# Jetbrains IDE",
Tags = new[]
{
"ide",
"c#",
"programming",
"code"
}
});
}
}
[Fact]
private void GetAllToolsAsync_Returns_AllTools()
{
var toolParameters = new ToolParameters();
using (var context = new AppDbContext(options))
{
ToolRepository toolRepository = new ToolRepository(context);
Task<PagedList<Tool>> tools = toolRepository.GetAllToolsAsync(toolParameters, false);
Assert.Equal(3, tools.Result.Count);
}
}
}
I've been stuck in this for 3 days, trying different tutorials with no success. How can i set up the tests for them to work?
Aucun commentaire:
Enregistrer un commentaire