我正在使用mog模拟一个LINQ to SQL的存储库,如下所示:
public static IProductsRepository MockProductsRepository(params Product[] prods){
// Generate an implementer of IProductsRepository at runtime using Moq
var mockProductsRepos = new Mock<IProductsRepository>();
mockProductsRepos.Setup(x => x.Products).Returns(prods.AsQueryable());
return mockProductsRepos.Object;
}
public interface IProductsRepository{
IQueryable<Product> Products { get; }
void SaveProduct(Product product);
void DeleteProduct(Product product);
}
如果我像这样使用它,我如何为Entity框架更改此函数:
public interface IProductsRepository : IEntities{
EntityState GetEntryState(object entry);
void SetEntryState(object entry, EntityState state);
void Commit();
}
public interface IEntities{
DbSet<Product> Products { get; set; }
}
现在我正在使用DbSet
。
答案 0 :(得分:2)
好吧,由于IProductsRepository
实现了IEntities
,你应该有一个
public DbSet<Product> Products { get; set; }
那里有属性,但我要做的是向Fetch
添加IProductRepository
方法,如
public interface IProductsRepository : IEntities
{
EntityState GetEntryState(object entry);
void SetEntryState(object entry, EntityState state);
void Commit();
// New method
IQueryable<Product> FetchAll();
}
然后,在MockProductsRepository
更改设置行,如下所示:
mockProductsRepos.Setup(x => x.FetchAll()).Returns(prods.AsQueryable());