I am using the ProjectTo<T>
method to get data from the DB.
names = _mapper.ProjectTo<ItemNameDto>(query).ToList();
In the unit tests I would like to mock this method to return a specific collection.
Based on the second answer to this question - I figured out I need to pass all parameters to the setup.
When I pass null to the second parameter - the setup is ok, but when I pass null
to the third one - the setup does not return the collection I want.
What value should I pass to the Expression parameter in this particular case? I really wouldn't like to leave it with It.IsAny<>
because it seems too broad to me. I would like to write a setup which reflects exactly my use case.
_mapperMock
.Setup(c => c.ProjectTo(
It.Is<IQueryable<Item>>(x => x.HaveTheSameElements(filteredItems)),
It.IsAny<object>(),
It.IsAny<Expression<Func<ItemNameDto, object>>[]>()))
.Returns(filteredItemNameDtos.AsQueryable());
This is the method signature I would like to setup.
IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, object parameters = null, params Expression<Func<TDestination, object>>[] membersToExpand);
To mock a call to ProjectTo
where no membersToExpand
are provided, you should use It.Is
:
_mapperMock
.Setup(c => c.ProjectTo(
It.Is<IQueryable<Item>>(x => x.HaveTheSameElements(filteredItems)),
It.IsAny<object>(),
It.Is<Expression<Func<ItemNameDto, object>>[]>(x => x.Length == 0)))
.Returns(filteredItemNameDtos.AsQueryable());
x => x.Length == 0
indicates an empty array, which the framework instantiates when no params
have been passed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.