私はジェネリックリポジトリのdbsetのwhere節をモックしようとしています。なぜSystem.NotSupported Exceptionを取得しているのかわかりません。 私はまったく新しいことを嘲笑しているので、なぜこれが起こっているのかわかりません。模擬dbset <T>ここでメソッドはnotSupportedExceptionを返します
private List<StubEntity> _data;
private Repository<StubEntity> _repository;
[TestInitialize]
public void TestInitialize()
{
_data = new List<StubEntity>
{
new StubEntity {Id = 1, Name = "Entity 1"},
...
};
var queryableData = _data.AsQueryable();
var mockDbSet = new Mock<DbSet<StubEntity>>();
mockDbSet
.Setup(m => m.Where(It.IsAny<Expression<Func<StubEntity, bool>>>()))
.Returns<Expression<Func<StubEntity, bool>>>(p => queryableData.Where(p));
var context = new Mock<StubContext>();
context.Setup(x => x.DbEntities).Returns(mockDbSet.Object);
context.Setup(x => x.Set<StubEntity>()).Returns(mockDbSet.Object);
_repository = new Repository<StubEntity>(context.Object);
}
私は唯一のテストFindメソッドは、単にコンテキストのWHERE句を呼び出す
[TestMethod]
public void Find_ReturnsProperEntity()
{
var entity = _repository.Find(s => s.Id == 1);
....
}
にwhere句をテストしています。
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return Context.Set<TEntity>().Where(predicate);
}
この回答をお持ちですか? http://stackoverflow.com/questions/25960192/mocking-ef-dbcontext-with-moq – cecilphillip