You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with .NET 6 and Dapper for connecting into a Oracle database. Now, I need to write a proper unit test in Moq to my repository method, but I'm facing a real trouble to do so. I'm sharing my code bellow, hope I can get some help with it!
1. EquipmentRepository
`
using Dapper;
using MyCompany.MyProject.Application.Interfaces.Repositories;
using MyCompany.MyProject.Application.Models.Equipments;
using MyCompany.MyProject.Domain.Entities;
using MyCompany.MyProject.Domain.Interfaces;
using System.Data;
namespace MyCompany.MyProject.Infra.Data.Repositories
{
public class EquipmentRepository : IEquipmentRepository
{
private readonly IUnitOfWork _unitOfWork;
public EquipmentRepository(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Equipment>> GetAllEquipmentsAsync(int transporterId)
{
var equipmentList = await _unitOfWork.SafeExecution(async (connection) =>
{
var parameters = new DynamicParameters();
parameters.Add("CD_TRANSP", transporterId);
var query = "SELECT * FROM MYDB.EQUIPMENT WHERE CD_TRANSP = :CD_TRANSP";
return (await connection.QueryAsync<Equipment>(query, parameters, commandType: CommandType.Text)).ToList();
});
return equipmentList;
}
}
I've got a solution for this: the problem is attached to .ToList() inside the lambda function. When I remove it, the unit test works perfectly!
I'm sharing the unit test, in case someone needs it in the future:
`[Fact]
public async void Should_Return_All_Equipments()
{
// Arrange
var expected = Builder.CreateListOfSize(5).Build();
_unitOfWork.Setup(u => u.SafeExecution(It.IsAny<Func<IDbConnection, Task<IEnumerable<Equipment>>>>()))
.ReturnsAsync(expected);
// Act
var result = await _repository.GetAllEquipmentsAsync(1);
// Assert
Assert.Equal(expected, result);
}`
Hello! Good afternoon!
I'm working with .NET 6 and Dapper for connecting into a Oracle database. Now, I need to write a proper unit test in Moq to my repository method, but I'm facing a real trouble to do so. I'm sharing my code bellow, hope I can get some help with it!
1. EquipmentRepository
`
using Dapper;
using MyCompany.MyProject.Application.Interfaces.Repositories;
using MyCompany.MyProject.Application.Models.Equipments;
using MyCompany.MyProject.Domain.Entities;
using MyCompany.MyProject.Domain.Interfaces;
using System.Data;
namespace MyCompany.MyProject.Infra.Data.Repositories
{
public class EquipmentRepository : IEquipmentRepository
{
private readonly IUnitOfWork _unitOfWork;
}
`
2. IUnitOfWork.cs
`
using System.Data;
namespace MyCompany.MyProject.Domain.Interfaces
{
public interface IUnitOfWork
{
Task SafeExecution(Func<IDbConnection, Task> func);
}
}
`
3. UnitOfWork.cs
`
using Microsoft.Extensions.Configuration;
using Oracle.ManagedDataAccess.Client;
using MyCompany.MyProject.Domain.Interfaces;
using System.Data;
namespace MyCompany.MyProject.Infra.Data
{
public class UnitOfWork : IUnitOfWork
{
private readonly IConfiguration _configuration;
}
`
The text was updated successfully, but these errors were encountered: