A test class to help with DapperWrapper and FakeItEasy
namespace NAMESPACE
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DapperWrapper;
using FluentAssertions;
using ImpromptuInterface;
public class FakeDbExecutor : IDbExecutor
{
private int executeReturnValue;
private string expectedSql;
private object expectedParam;
private IDbTransaction expectedTransaction;
private int? expectedCommandTimeout;
private CommandType? expectedCommandType;
private string actualSql;
private object actualParam;
private IDbTransaction actualTransaction;
private int? actualCommandTimeout;
private CommandType? actualCommandType;
public bool WasDisposed { get; private set; }
public void SetExecuteReturnValue(int value)
{
executeReturnValue = value;
}
public void ExpectedExecute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null,
CommandType? commandType = null)
{
expectedSql = sql;
expectedParam = param;
expectedTransaction = transaction;
expectedCommandTimeout = commandTimeout;
expectedCommandType = commandType;
}
public int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null,
CommandType? commandType = null)
{
actualSql = sql;
actualParam = param;
actualTransaction = transaction;
actualCommandTimeout = commandTimeout;
actualCommandType = commandType;
return executeReturnValue;
}
public void ExecuteMustHaveHappened()
{
actualSql.Should().Be(expectedSql);
DynamicEquals(actualParam, expectedParam).Should().BeTrue("the parameters did not match");
actualTransaction.Should().Be(expectedTransaction);
actualCommandTimeout.Should().Be(expectedCommandTimeout);
actualCommandType.Should().Be(expectedCommandType);
}
public IEnumerable<dynamic> Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true,
int? commandTimeout = null, CommandType? commandType = null)
{
throw new NotImplementedException();
}
public IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true,
int? commandTimeout = null, CommandType? commandType = null)
{
throw new NotImplementedException();
}
public void Dispose()
{
WasDisposed = true;
}
private bool DynamicEquals(object obj1, object obj2)
{
var list1 = Impromptu.GetMemberNames(obj1);
list1 = list1.OrderBy(m => m);
var list2 = Impromptu.GetMemberNames(obj2);
list2 = list2.OrderBy(m => m);
var list = list1 as IList<string> ?? list1.ToList();
if (!list.SequenceEqual(list2))
return false;
return list.All(memberName => Impromptu.InvokeGet(obj1, memberName).Equals(Impromptu.InvokeGet(obj2, memberName)));
}
}
}