我正在尝试使用Moq模拟以下方法:
public interface IGenericRepository<TEntity> where TEntity : class
{
...
IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");
}
它以这种方式初始化:
_invoiceRepository = new SqlGenericRepository<InvoiceEntity>(Context);
无论参数如何,该方法都应该始终返回一个列表。
我试过
_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>()).Returns(...)
和
_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<Expression<Func<InvoiceEntity, bool>>>())).Returns(...)
但两者都不起作用。
答案 0 :(得分:1)
如果你的方法是:
IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "");
你的模拟必须是这样的:
_invoiceRepositoryMock.Setup(m => m.Get(
It.IsAny<Expression<Func<InvoiceEntity, bool>>>(),
It.IsAny<Func<IQueryable<InvoiceEntity>, IOrderedQueryable<InvoiceEntity>>>(),
It.IsAny<string>())).Returns(...)
答案 1 :(得分:1)
假设
var _invoiceRepositoryMock = new Mock<InvoiceEntity>();
然后设置
_invoiceRepositoryMock
.Setup(m => m.Get(
It.IsAny<Expression<Func<InvoiceEntity, bool>>>(),
It.IsAny<Func<IQueryable<InvoiceEntity>, IOrderedQueryable<InvoiceEntity>>>(),
It.IsAny<string>()))
.Returns(...);
或更具体的
_invoiceRepositoryMock
.Setup(m => m.Get(It.IsAny<Expression<Func<InvoiceEntity, bool>>>(), null, string.Empty))
.Returns(...);
答案 2 :(得分:0)
你可以尝试这样尝试,因为该方法有4个参数,尝试在模拟中给出4个参数_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>(),It.IsAny<>(),It.IsAny<>(),It.IsAny<>()).Returns(...