0

我正在尝试最小化我的通用存储库。我有一个名为 SingleOrDefaultIncluding 的方法,如下所示:

T SingleOrDefaultIncluding(Expression<Func<T, bool>> wherePredicate, params Expression<Func<T, object>>[] includes);

现在在我的单元测试中,我像这样设置 Moq:

 _agreementRepositoryMock.Setup(m => m.SingleOrDefaultIncluding
                                                (
                                                    It.IsAny<Expression<Func<Agreement, bool>>>(),
                                                    It.IsAny<Expression<Func<Agreement, object>>>(),
                                                    It.IsAny<Expression<Func<Agreement, object>>>()
                                                )
            ).Returns(AgreementMocks.GetOne());

我可以以某种方式指定传递给 SingleOrDefaultIncluding 的任何参数都应该返回我的协议吗?为了完成这项工作,我必须确保准确地传递 3 个参数,就像我正在测试的方法一样。或者我可以以某种方式将数组传递给 Moq 吗?

4

1 回答 1

1

也许使用您不“扩展”的形式params?就像是:

_agreementRepositoryMock.Setup(m => m.SingleOrDefaultIncluding
  (
    It.IsAny<Expression<Func<Agreement, bool>>>(),
    It.IsAny<Expression<Func<Agreement, object>>[]>()
  )
  ).Returns(AgreementMocks.GetOne());

注意[]以上。未经测试。

如果我误解了,要冻结参数的数量,也许可以尝试:

_agreementRepositoryMock.Setup(m => m.SingleOrDefaultIncluding
  (
    It.IsAny<Expression<Func<Agreement, bool>>>(),
    It.Is<Expression<Func<Agreement, object>>[]>(array => array.Length == 2)
  )
  ).Returns(AgreementMocks.GetOne());

使用It.Is<>而不是It.IsAny<>.

于 2013-09-18T11:53:26.623 回答