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

QueryAsync returns no records #91

Open
molynerd opened this issue Aug 22, 2023 · 1 comment
Open

QueryAsync returns no records #91

molynerd opened this issue Aug 22, 2023 · 1 comment

Comments

@molynerd
Copy link

molynerd commented Aug 22, 2023

Either I've configured something incorrectly, or QueryAsync doesn't work.

  • xunit 2.4.2
  • Dapper 2.0.151
  • Moq 4.20.69
  • Moq.Dapper 1.0.7
public class SomethingToMock
{
    private string Key;

    public SomethingToMock(string key)
    {
        Key = key;
    }
}

[Fact]
public async Task DoesMoqDapperWork()
{
    var connection = new Mock<IDbConnection>();

    var expected = new[]
    {
        new SomethingToMock ("asdf")
    };

    connection.SetupDapperAsync(c => c.QueryAsync<SomethingToMock>(It.IsAny<string>(), null, null, null, null))
        .ReturnsAsync(expected);

    var data = await connection.Object.QueryAsync<SomethingToMock>("foo");
    Assert.Equal(expected.Length, data.Count());
}
@Monkey-D-Luisi
Copy link

Monkey-D-Luisi commented Dec 13, 2024

Same problem, did you resolve?

internal class FeatureToggle
{


    internal required string Key { get; set; }
    internal required bool IsAvailable { get; set; }
    internal bool? IsEnabledForAll {  get; set; }
    internal IEnumerable<int>? EnabledOnLicenseTypes { get; set; }
    internal IEnumerable<int>? EnabledOnLicenseIds { get; set; }


    internal bool IsEnabledFor(int licenseTypeId, int licenseId)
    {
        return
            IsAvailable
            && ((IsEnabledForAll ?? false)
            || (EnabledOnLicenseTypes?.Contains(licenseTypeId) ?? false)
            || (EnabledOnLicenseTypes?.Contains(licenseId) ?? false));
    }
}
internal class SqlFeatureToggleProvider : IFeatureToggleProvider
{


    private readonly Func<IDbConnection> connectionFactory;


    public SqlFeatureToggleProvider(Func<IDbConnection> connectionFactory)
    {
        this.connectionFactory = connectionFactory;
    }


    public async Task<IEnumerable<FeatureToggle>> ListFeatureTogglesAsync()
    {
        using var connection = connectionFactory();
        var query = "HS_List_FeatureToggles";

        var featureToggles = await connection.QueryAsync<FeatureToggle>(
            query,
            commandType: CommandType.StoredProcedure
        );

        return featureToggles;
    }
}
[TestFixture(Category = "Unit")]
internal class SqlFeatureToggleProviderShould
{


    [Test]
    public async Task Return_the_list_of_feature_toggles()
    {
        var featureKey = "testKey";

        var mockConnection = new Mock<IDbConnection>();
        mockConnection
            .SetupDapperAsync(c => c.QueryAsync<FeatureToggle>(
                It.IsAny<string>(),
                It.IsAny<object?>(),
                It.IsAny<IDbTransaction?>(),
                It.IsAny<int?>(),
                It.IsAny<CommandType?>()))
            .ReturnsAsync(new List<FeatureToggle>
            {
                new FeatureToggle()
                {
                    Key = featureKey,
                    IsAvailable = true,
                    IsEnabledForAll = true
                }
            });

        var provider = new SqlFeatureToggleProvider(() => mockConnection.Object);

        var result = await provider.ListFeatureTogglesAsync();

        result.Should().NotBeEmpty();
        result.FirstOrDefault().IsEnabledFor(0, 0).Should().BeTrue();
    }
}

I don't know if my problem provides from having the connection inside a function, i done it to do the class testable...

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

2 participants