通用存储库的模拟方法
Mocking method of generic repository
我正在尝试使用 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);
无论参数如何,方法都应该 return 一个列表。
我试过了
_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>()).Returns(...)
和
_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<Expression<Func<InvoiceEntity, bool>>>())).Returns(...)
但两者均无效。
如果您的方法是:
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(...)
你能试试这样吗,因为该方法有 4 个参数,请尝试在 mock 中也提供 4 个参数 _invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>(),It.IsAny<>(),It.IsAny<>(),It.IsAny<>()).Returns(...
假设
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(...);
我正在尝试使用 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);
无论参数如何,方法都应该 return 一个列表。
我试过了
_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>()).Returns(...)
和
_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<Expression<Func<InvoiceEntity, bool>>>())).Returns(...)
但两者均无效。
如果您的方法是:
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(...)
你能试试这样吗,因为该方法有 4 个参数,请尝试在 mock 中也提供 4 个参数 _invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>(),It.IsAny<>(),It.IsAny<>(),It.IsAny<>()).Returns(...
假设
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(...);