Skip to content

Instantly share code, notes, and snippets.

@lukencode
Created September 2, 2013 00:19
Show Gist options
  • Save lukencode/6408204 to your computer and use it in GitHub Desktop.
Save lukencode/6408204 to your computer and use it in GitHub Desktop.
public class DataMockHelpers
{
public Mock<IQeraUnit> GetMockUnit()
{
var unit = new Mock<IQeraUnit>();
return unit;
}
public Mock<TRepo> MockRepo<TRepo, TEntity>(List<TEntity> seedData = null, bool callBase = false, MockBehavior behavior = MockBehavior.Default)
where TRepo : class, IRepo<TEntity>
where TEntity : class
{
var repo = new Mock<TRepo>(behavior) { CallBase = callBase };
var backingStore = seedData ?? new List<TEntity>();
repo.Setup(x => x.All()).Returns(backingStore.AsQueryable());
repo.Setup(x => x.Insert(It.IsAny<TEntity>())).Callback((TEntity item) => backingStore.Add(item));
repo.Setup(x => x.Single(It.IsAny<Expression<Func<TEntity, bool>>>())).Returns((Expression<Func<TEntity, bool>> filter) => backingStore.AsQueryable().FirstOrDefault(filter));
repo.Setup(x => x.Delete(It.IsAny<TEntity>())).Callback((TEntity item) => backingStore.Remove(item));
return repo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment