DbMocker 1.1.0

This .NET library simplifies data mocking for UnitTests, to avoid a connection to a relational database.
DbMocker use the standard Microsoft .NET DbConnection object. So, you can mock any toolkit,
including EntityFramework, Dapper or ADO.NET; And for all database servers (SQL Server, Oracle, SQLite).

var conn = new MockDbConnection();
conn.Mocks.WhenAny()..ReturnsScalar(14);

There is a newer version of this package available.
See the version list below for details.
Install-Package DbMocker -Version 1.1.0
dotnet add package DbMocker --version 1.1.0
<PackageReference Include="DbMocker" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DbMocker --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

DbMocker - Simple Database Mocker for UnitTests

Introduction

This .NET library simplifies data mocking for UnitTests, to avoid a connection to a relational database.
DbMocker use the standard Microsoft .NET DbConnection object. So, you can mock any toolkit,
including EntityFramework, Dapper or ADO.NET; And for all database servers (SQL Server, Oracle, SQLite).

First, add the DbMocker NuGet packages.
Second, mock you SQL requests using this library like this.

Please, contact me if you want other features or to solve bugs.

// Sample method from your DataService
public int GetNumberOfEmployees(DbConnection connection)
{
    using (var cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT COUNT(*) FROM Employees";
        return Convert.ToInt32(cmd.ExecuteScalar());
    }
}

[TestMethod]
public void UnitTest1()
{
    var conn = new MockDbConnection();

    // When a specific SQL command is detected,
    // Don't execute the query to your SQL Server,
    // But returns this MockTable.
    conn.Mocks
        .When(cmd => cmd.CommandText.StartsWith("SELECT COUNT(*)") &&
                     cmd.Parameters.Count() == 0)
        .ReturnsTable(MockTable.WithColumns("Count")
                               .AddRow(14));

    // Call your "classic" methods to tests
    int count = GetNumberOfEmployees(conn);

    Assert.AreEqual(14, count);
}

Road map

  • DataSets are not yet implemented.

DbMocker - Simple Database Mocker for UnitTests

Introduction

This .NET library simplifies data mocking for UnitTests, to avoid a connection to a relational database.
DbMocker use the standard Microsoft .NET DbConnection object. So, you can mock any toolkit,
including EntityFramework, Dapper or ADO.NET; And for all database servers (SQL Server, Oracle, SQLite).

First, add the DbMocker NuGet packages.
Second, mock you SQL requests using this library like this.

Please, contact me if you want other features or to solve bugs.

// Sample method from your DataService
public int GetNumberOfEmployees(DbConnection connection)
{
    using (var cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT COUNT(*) FROM Employees";
        return Convert.ToInt32(cmd.ExecuteScalar());
    }
}

[TestMethod]
public void UnitTest1()
{
    var conn = new MockDbConnection();

    // When a specific SQL command is detected,
    // Don't execute the query to your SQL Server,
    // But returns this MockTable.
    conn.Mocks
        .When(cmd => cmd.CommandText.StartsWith("SELECT COUNT(*)") &&
                     cmd.Parameters.Count() == 0)
        .ReturnsTable(MockTable.WithColumns("Count")
                               .AddRow(14));

    // Call your "classic" methods to tests
    int count = GetNumberOfEmployees(conn);

    Assert.AreEqual(14, count);
}

Road map

  • DataSets are not yet implemented.

Release Notes

https://github.com/Apps72/Apps72.Dev.Data.DbMocker

  • .NETCoreApp 2.1

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.6.0 1,369 4/17/2019
1.5.0 597 9/2/2018
1.4.1-alpha 132 8/29/2018
1.4.0 187 8/5/2018
1.3.0 201 8/2/2018
1.2.0 180 7/31/2018
1.1.0 267 7/15/2018
Show less