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

Add MockDbDataSource #34

Open
lechu445 opened this issue Feb 3, 2024 · 3 comments
Open

Add MockDbDataSource #34

lechu445 opened this issue Feb 3, 2024 · 3 comments

Comments

@lechu445
Copy link

lechu445 commented Feb 3, 2024

.NET 7 introduced a new abstraction DbDataSource. Please add possibility to mock it.

dotnet/runtime#64812

@dvoituron
Copy link
Contributor

Hi. I will check how it is possible. But you could create a new PR to improve the lib 😊

@dvoituron
Copy link
Contributor

I published a 2.0.1-preview version, adding a new class MockDbDataSource, to try to find a solution.

[TestMethod]
public async Task Mock_DataSource_Default_Test()
{
    await using var dataSource = new MockDbDataSource();
    await using var connection = await dataSource.OpenConnectionAsync() as MockDbConnection;

    connection.Mocks
                .When(c => c.CommandText.Contains("SELECT"))
                .ReturnsScalar(14);

    using (var cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT * FROM EMP WHERE ID = @ID";
        var result = cmd.ExecuteScalar();

        Assert.AreEqual(14, result);
    }
}

Could you tell me how you use the DbDataSource?
Do you have an example that I can include in my unit test project?

@lechu445
Copy link
Author

lechu445 commented Feb 6, 2024

I also use it in this way:

[TestMethod]
public async Task Mock_DataSource_CreateCommand_Test()
{
    await using var dataSource = new MockDbDataSource();
    await using var connection = await dataSource.OpenConnectionAsync() as MockDbConnection;

    connection.Mocks
                .When(c => c.CommandText.Contains("SELECT"))
                .ReturnsScalar(14);

    using (var cmd = dataSource.CreateCommand("SELECT * FROM EMP WHERE ID = @ID"))
    {
        var result = cmd.ExecuteScalar();

        Assert.AreEqual(14, result);
    }
}

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