我想实现一个扩展方法来初始化我的MVC3应用程序的我的Moq存储库。我有一个仓库接口:通用接口的扩展方法不显示在子类
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
//Methods
}
我有几类,如UserRepository其实现此接口:
public interface IUserRepository : IRepository<User>
{
//Specific methods for User repository
}
public class UserRepository : EfRepositoryBase<User>, IUserRepository
{
}
EfRepositoryBase是我的仓库基础类,它提供了我的仓库一般方法。在我的单元测试中,我想为每种类型的存储库创建一个扩展方法来检索模拟存储库。我试着加入这样的扩展方法:
public static class RepositoryHelpers
{
public static Mock<IRepository<T>> GetMockRepository<T>(this IRepository<T> repository, params T[] items) where T : class
{
Mock<IRepository<T>> mock = new Mock<IRepository<T>>();
mock.Setup(m => m.GetAll()).Returns(items.AsQueryable());
return mock;
}
}
但是,这似乎并不奏效。我期待使用UserRepository.GetMockRepository(...)来检索初始化的模拟库,但该方法不会显示在UserRepository上。
UPDATE
我得到它像新的一样UserRepository()工作。GetMockRepository(),有什么办法让可以作为一个静态方法,所以我不这个方法有新的一个UserRepository?
这就是我之前使用它的方式,我只是想我可以把它作为一个扩展可用方法。 – b3n 2011-02-28 03:33:51