The examples I\'ve seen for using moq for a repository only show how to mock things being returned. I have a somewhat strange requirement: when a query is executed, if a conditi
Try to use fake in memory repository instead of moq, for example universal generic repository for all entities:
public interface IInMemoryRepository where T : class
{
IQueryable GetAll();
void Create(T item);
void Update(T item);
T GetItem(Expression> expression);
void Delete(T item);
}
public class InMemoryRepository : IInMemoryRepository where T : class
{
private int _incrementer = 0;
public Dictionary List = new Dictionary();
public IQueryable GetAll()
{
return List.Select(x => x.Value).AsQueryable();
}
public void Create(T item)
{
_incrementer++;
item.GetType().GetProperties().First(p => p.Name == "Id").SetValue(item, _incrementer, null);
List.Add(_incrementer, item);
}
public void Update(T item)
{
var key = (int)item.GetType().GetProperties().First(p => p.Name == "Id").GetValue(item, null);
List[key] = item;
}
public T GetItem(Expression> expression)
{
return List.Select(x => x.Value).SingleOrDefault(expression.Compile());
}
public void Delete(T item)
{
var key = (int)item.GetType().GetProperties().First(p => p.Name == "Id").GetValue(item, null);
List.Remove(key);
}
}