Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble to unit test a lambda expression with Moq #1387

Closed
Henrickqt opened this issue Aug 10, 2023 · 1 comment
Closed

Trouble to unit test a lambda expression with Moq #1387

Henrickqt opened this issue Aug 10, 2023 · 1 comment

Comments

@Henrickqt
Copy link

Henrickqt commented Aug 10, 2023

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;

    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;
    }
}

}
`

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;

    public UnitOfWork(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public async Task<T> SafeExecution<T>(Func<IDbConnection, Task<T>> func)
    {
        try
        {
            using (OracleConnection connection = new(_configuration["ConnectionString"]))
            {
                connection.Open();
                return await func(connection);
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
}

}
`

@Henrickqt
Copy link
Author

Hello! Good morning!

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);
    }`

Thank you anyway, and sorry for the disturbance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant