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

Testing Query with string array parameter #93

Open
Dev99987 opened this issue Dec 6, 2023 · 0 comments
Open

Testing Query with string array parameter #93

Dev99987 opened this issue Dec 6, 2023 · 0 comments

Comments

@Dev99987
Copy link

Dev99987 commented Dec 6, 2023

Hi,

I have been trying to test one of my Dapper repository methods which uses a string array as one of it's input parameters.
The method itself works great but when I run the test it fails with the following error:
System.ArgumentNullException : Value cannot be null. (Parameter 'input')

By removing the array from the input parameters the error goes away.
The error occurs at the Query call.

The method I'm testing:

public IEnumerable<DataModel> GetData(string[] stringarrayparameter)
{
    using var log = _logger.CurrentMethodStart();

    var sql = @"
        select * from SameTable where SameColumn IN @stringarrayparameter
    ";

    using var connection = _SqlConnectionFactory.CreateConnection();

    connection.Open();

    return connection.Query<DataModel>(sql, new { @stringarrayparameter});
}

Dapper setup:

public virtual ISqlConnectionFactory GetMockSqlConnectionFactory()
{
    var mockDbConnectionFactory = new Mock<ISqlConnectionFactory>();
    var mockDbConnection = new Mock<IDbConnection>();
    var stringarrayparameter = new[] { "value" };

    var returnValue = new[]
    {
        new ReturnValue()
        {
            a = 1,
            b = 2,
            c = 3
        }
    };

    mockDbConnection.SetupDapper(c => c.Query<DataModel>(It.IsAny<string>(), new { @stringarrayparameter }, null, true, null, null)).Returns(ReturnValue);

    mockDbConnectionFactory.Setup(x => x.CreateConnection()).Returns(mockDbConnection.Object);

    return mockDbConnectionFactory.Object;
}

The test I'm running:

[Fact]
public void Repository_GetData_Ok()
{
    #region ARRANGE

    var sqlConnectionFactory = GetMockSqlConnectionFactory();
    var logger = GetLogger<Repository>(_output);
    var stringarrayparameter = new[] { "value" };

    #endregion ARRANGE

    #region ACT

    var ctr = new Repository(logger, sqlConnectionFactory);
    var act = ctr.GetData(stringarrayparameter);

    #endregion ACT

    #region ASSERT

    var dataModels = act.ToList();
    dataModels.Should().NotBeNull();

    #endregion ASSERT
}
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