https://github.com/unosd/moq.dapper
Moq extensions for Dapper methods.
https://github.com/unosd/moq.dapper
dapper moq test
Last synced: 8 months ago
JSON representation
Moq extensions for Dapper methods.
- Host: GitHub
- URL: https://github.com/unosd/moq.dapper
- Owner: UnoSD
- License: gpl-2.0
- Created: 2017-03-12T10:33:43.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2025-01-31T14:57:32.000Z (12 months ago)
- Last Synced: 2025-05-14T18:05:22.928Z (8 months ago)
- Topics: dapper, moq, test
- Language: C#
- Size: 98.6 KB
- Stars: 178
- Watchers: 12
- Forks: 79
- Open Issues: 42
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Moq.Dapper
Moq extensions for Dapper methods.
[](https://www.nuget.org/packages/Moq.Dapper)
[]()
# Example usage
Mocking a call to `Query` with a simple type:
```csharp
[Test]
public void QueryGeneric()
{
var connection = new Mock();
var expected = new[] { 7, 77, 777 };
connection.SetupDapper(c => c.Query(It.IsAny(), null, null, true, null, null))
.Returns(expected);
var actual = connection.Object.Query("", null, null, true, null, null).ToList();
Assert.That(actual.Count, Is.EqualTo(expected.Length));
Assert.That(actual, Is.EquivalentTo(expected));
}
```
Mocking a call to `Query` with a complex type:
```csharp
[Test]
public void QueryGenericComplexType()
{
var connection = new Mock();
var expected = new[]
{
new ComplexType { StringProperty = "String1", IntegerProperty = 7 },
new ComplexType { StringProperty = "String2", IntegerProperty = 77 },
new ComplexType { StringProperty = "String3", IntegerProperty = 777 }
};
connection.SetupDapper(c => c.Query(It.IsAny(), null, null, true, null, null))
.Returns(expected);
var actual = connection.Object.Query("", null, null, true, null, null).ToList();
Assert.That(actual.Count, Is.EqualTo(expected.Length));
foreach (var complexObject in expected)
{
var match = actual.Where(co => co.StringProperty == complexObject.StringProperty && co.IntegerProperty == complexObject.IntegerProperty);
Assert.That(match.Count, Is.EqualTo(1));
}
}
```
Mocking a call to `ExecuteScalar`:
```csharp
[Test]
public void ExecuteScalar()
{
var connection = new Mock();
const int expected = 77;
connection.SetupDapper(c => c.ExecuteScalar(It.IsAny(), null, null, null, null))
.Returns(expected);
var actual = connection.Object.ExecuteScalar("", null, null, null);
Assert.That(actual, Is.EqualTo(expected));
}
```
Mocking a call to `QueryAsync`
```csharp
[Test]
public async Task QueryAsyncGeneric()
{
var connection = new Mock();
var expected = new[] { 7, 77, 777 };
connection.SetupDapperAsync(c => c.QueryAsync(It.IsAny(), null, null, null, null))
.ReturnsAsync(expected);
var actual = (await connection.Object.QueryAsync("", null, null, null, null)).ToList();
Assert.That(actual.Count, Is.EqualTo(expected.Length));
Assert.That(actual, Is.EquivalentTo(expected));
}
```
Mocking a call to `ExecuteScalarAsync`:
```csharp
[Test]
public void ExecuteScalarAsync()
{
var connection = new Mock();
const string expected = "Hello";
connection.SetupDapperAsync(c => c.ExecuteScalarAsync(It.IsAny(), null, null, null, null))
.ReturnsAsync(expected);
var actual = connection.Object
.ExecuteScalarAsync("")
.GetAwaiter()
.GetResult();
Assert.That(actual, Is.EqualTo(expected));
}
```