DbMocker 1.10.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
.When(cmd => cmd.CommandText.Contains("SELECT COUNT(*) FROM EMP"))
.ReturnsScalar(14);
```
See the version list below for details.
Install-Package DbMocker -Version 1.10.0
dotnet add package DbMocker --version 1.10.0
<PackageReference Include="DbMocker" Version="1.10.0" />
paket add DbMocker --version 1.10.0
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)[https://www.nuget.org/packages/DbMocker].
Next, instanciate a MockDbConnection
and mock you SQL requests using a condition and return a DataTable.
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 database engine (SQL Server, Oracle, SQLite, ...),
// But returns this _Table_.
conn.Mocks
.When(cmd => cmd.CommandText.StartsWith("SELECT") &&
cmd.Parameters.Count() == 0)
.ReturnsTable(MockTable.WithColumns("Count")
.AddRow(14));
// Call your "classic" methods to tests
int count = GetNumberOfEmployees(conn);
Assert.AreEqual(14, count);
}
See https://apps72.com for more information.
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)[https://www.nuget.org/packages/DbMocker].
Next, instanciate a MockDbConnection
and mock you SQL requests using a condition and return a DataTable.
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 database engine (SQL Server, Oracle, SQLite, ...),
// But returns this _Table_.
conn.Mocks
.When(cmd => cmd.CommandText.StartsWith("SELECT") &&
cmd.Parameters.Count() == 0)
.ReturnsTable(MockTable.WithColumns("Count")
.AddRow(14));
// Call your "classic" methods to tests
int count = GetNumberOfEmployees(conn);
Assert.AreEqual(14, count);
}
See https://apps72.com for more information.
Release Notes
https://github.com/Apps72/DbMocker
Dependencies
-
.NETStandard 2.0
- Microsoft.SqlServer.Management.SqlParser (>= 160.20216.14)
Used By
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version History
Version | Downloads | Last updated |
---|---|---|
1.17.0 | 0 | 1/25/2021 |
1.16.0 | 46 | 1/23/2021 |
1.15.0 | 49 | 1/13/2021 |
1.14.0 | 26 | 1/13/2021 |
1.13.0 | 99 | 12/22/2020 |
1.12.0 | 52 | 12/14/2020 |
1.11.0 | 98 | 11/23/2020 |
1.10.0 | 71 | 11/23/2020 |
1.9.0 | 62 | 11/12/2020 |
1.8.0 | 72 | 11/11/2020 |
1.7.0-Preview2 | 214 | 5/20/2020 |
1.7.0-Preview1 | 140 | 5/20/2020 |
1.6.0 | 14,715 | 4/17/2019 |
1.5.0 | 814 | 9/2/2018 |
1.4.1-alpha | 304 | 8/29/2018 |
1.4.0 | 372 | 8/5/2018 |
1.3.0 | 388 | 8/2/2018 |
1.2.0 | 372 | 7/31/2018 |
1.1.0 | 473 | 7/15/2018 |