簡體   English   中英

Moq通用存儲庫-TARGETPARAMETERCOUNTEXCEPTION

[英]Moq generic Repository - TARGETPARAMETERCOUNTEXCEPTION

我正在模擬通用存儲庫,並且剛剛在Retrieve方法中添加了第二個參數,從而允許我傳遞對象屬性的包含字符串,但我對如何模擬該特性有些困惑,並且正在獲取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);

最小起訂量起作用,第二個不起作用。

Returns方法中,您需要將模擬方法的所有參數類型指定為通用參數。

因此,您在第二個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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM