Assemblies to assist testing Dapper database invocations
connection.Query<int>(@"select count(*)
from Table
where Name = @name", new { name = "anything" })
using Dapper.MoqTests;
public void Test()
{
var connection = new MockDbConnection();
var repository = new MyTestRepository(connection);
repository.ReadNames();
connection.Verify(c => c.Query<int>(@"select count(*)
from Table
where Name = @name", new { name = "anything" }));
}
using Dapper.MoqTests;
public void Test()
{
var connection = new MockDbConnection();
var repository = new MyTestRepository(connection);
repository.DeleteSomething();
connection.Verify(c => c.Execute(@"delete
from Table
where Name = @name", new { name = "to-be-deleted" }));
}
using Dapper.MoqTests;
using System.Data;
public void Test()
{
var connection = new MockDbConnection();
var repository = new MyTestRepository(connection);
var data = new DataTable
{
Columns =
{
"name"
},
Rows =
{
"Bloggs",
"Smith"
}
};
connection
.Setup(c => c.Query<string>(@"select name
from Table
where Enabled = 1"))
.Return(new DataTableReader(data))
repository.ReadNames();
connection.Verify(c => c.Query<string>(@"select name
from Table
where Enabled = 1", It.IsAny<object>()));
}
- MockDbConnection implements
IDbConnection
- Supports testing of
Query
andQuerySingle
- Supports testing of
Execute
- Compares SQL test case insensitively, ignoring empty lines and leading/trailing white-space
- Compares parameter anonymous objects from different assemblies
- Testing framework isn't restricted, can by NUnit, MsTest or anything else
- Supports Strict execution, pass in the appropriate parameter to the
MockDbConnection
constructor
- If you have not set-up a
Query<T>()
orQuerySingle<T>()
, you can only verify byQuery<object>()
orQuerySingle<object>()
Raise an issue and I'll see what I can do
Happy testing, let me know if you see any issues and I'll do what I can to resolve them.