2013-07-03 26 views
1

我正在嘲笑一個通用的知識庫,並剛剛向我的Retrieve方法添加了第二個參數,允許我傳遞包含對象屬性的字符串,我有點卡在如何模擬這一點,並得到一個TargetParameterCountExceptionMoq通用知識庫 - TARGETPARAMETERCOUNTEXCEPTION

如果有人能讓我朝正確的方向發展,那會很棒。

接口:

IQueryable<T> Retrieve(Expression<Func<T, bool>> predicate); 

IQueryable<T> Retrieve(Expression<Func<T, bool>> predicate, IEnumerable<string> includes); 

起訂量:

var mActionRepository = new Mock<IRepository<ContainerAction>>(); 
mActionRepository.Setup(m => m.Retrieve(It.IsAny<Expression<Func<ContainerAction, bool>>>())) 
    .Returns<Expression<Func<ContainerAction, bool>>>(queryable.Where); 

mActionRepository.Setup(m => m.Retrieve(It.IsAny<Expression<Func<ContainerAction, bool>>>(), It.IsAny<IEnumerable<string>>())) 
    .Returns<Expression<Func<ContainerAction, bool>>>(queryable.Where); 

第一起訂量的工作,第二個沒有。

回答

1

Returns方法中,您需要指定mocked方法的所有參數類型作爲泛型參數。

因此,您在第二個Returns呼叫中缺少IEnumerable<string>,這就是爲什麼您會得到TargetParameterCountException

所以你的第二個Returns應該是這樣的:

mActionRepository.Setup(m => m.Retrieve(
    It.IsAny<Expression<Func<ContainerAction, bool>>>(), 
    It.IsAny<IEnumerable<string>>())) 
    .Returns<Expression<Func<ContainerAction, bool>>, IEnumerable<string>>(
     (predicate, includes) => queryable.Where(predicate)); 
+0

爲十分感謝 –