Moq 抛出异常,指出表达式树可能不包含使用可选参数的调用或调用
Moq throws exceptions stating that an expression tree may not contain a call or invocation that uses optional arguments
我正在编写 NUnit 测试用例。我正在编写测试用例以获得一些值。以下是我的设置。
[Test]
public async Task GetGeographyList_StateUnderTest_ExpectedBehavior()
{
// Arrange
var geographyBusinessLogic = this.CreateGeographyBusinessLogic();
Geography geographyObj = new Geography() { Id = 101, Country = "India", Region = "APAC", SubRegion = "Asia South" };
IEnumerable<Geography> geographyObjList = new List<Geography>() { geographyObj };
//this.geographyRepository.setupGetAsync<Geography>(geographyObjList);
this.geographyRepository.Setup(
x => x.GetAsync()).ReturnsAsync(geographyObjList);
// Act
var result = await geographyBusinessLogic.GetGeographyList();
// Assert
Assert.IsNotNull(result);
}
在上面的代码中,x => x.GetAsync()
抛出一个错误:
An expression tree may not contain a call or invocation that uses optional arguments
下面是我对geographyBusinessLogic.GetGeographyList()
的实现:
public async Task<IEnumerable<GeographyEntity>> GetGeographyList()
{
var listOfGeographies = await this.GeographyRepository.GetAsync().ConfigureAwait(false);
IEnumerable<GeographyEntity> result = from o in listOfGeographies
select new GeographyEntity
{
Id = o.Id,
Country = o.Country,
Region = o.Region,
SubRegion = o.SubRegion
};
return result;
}
下面是GetAsync
方法的实现:
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
谁能帮我理解这个错误,谁能告诉我我在这里遗漏了什么?任何帮助将不胜感激。
您需要在设置模拟时填写所有可选参数。例如:
this.geographyRepository.Setup(x => x.GetAsync(
It.IsAny<Expression<Func<Geography, bool>>>(),
It.IsAny<Func<IQueryable<Geography>, IOrderedQueryable<Geography>>>(),
It.IsAny<Expression<Func<Geography, object>>[]>())).ReturnsAsync(geographyObjList);
我正在编写 NUnit 测试用例。我正在编写测试用例以获得一些值。以下是我的设置。
[Test]
public async Task GetGeographyList_StateUnderTest_ExpectedBehavior()
{
// Arrange
var geographyBusinessLogic = this.CreateGeographyBusinessLogic();
Geography geographyObj = new Geography() { Id = 101, Country = "India", Region = "APAC", SubRegion = "Asia South" };
IEnumerable<Geography> geographyObjList = new List<Geography>() { geographyObj };
//this.geographyRepository.setupGetAsync<Geography>(geographyObjList);
this.geographyRepository.Setup(
x => x.GetAsync()).ReturnsAsync(geographyObjList);
// Act
var result = await geographyBusinessLogic.GetGeographyList();
// Assert
Assert.IsNotNull(result);
}
在上面的代码中,x => x.GetAsync()
抛出一个错误:
An expression tree may not contain a call or invocation that uses optional arguments
下面是我对geographyBusinessLogic.GetGeographyList()
的实现:
public async Task<IEnumerable<GeographyEntity>> GetGeographyList()
{
var listOfGeographies = await this.GeographyRepository.GetAsync().ConfigureAwait(false);
IEnumerable<GeographyEntity> result = from o in listOfGeographies
select new GeographyEntity
{
Id = o.Id,
Country = o.Country,
Region = o.Region,
SubRegion = o.SubRegion
};
return result;
}
下面是GetAsync
方法的实现:
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
谁能帮我理解这个错误,谁能告诉我我在这里遗漏了什么?任何帮助将不胜感激。
您需要在设置模拟时填写所有可选参数。例如:
this.geographyRepository.Setup(x => x.GetAsync(
It.IsAny<Expression<Func<Geography, bool>>>(),
It.IsAny<Func<IQueryable<Geography>, IOrderedQueryable<Geography>>>(),
It.IsAny<Expression<Func<Geography, object>>[]>())).ReturnsAsync(geographyObjList);