Reputation: 53
Hi I am having an issue with my moq when I try to return data, I saw this questtion with a solution but it doesn't work on my side.
Can anyone please tell me what I'm doing wrong
Here is my respository
private readonly IRepository<SomeClass<MyObject>> repository;
public MyObjectRepository(IRepositoryFactory factory)
{
repository = factory.RepositoryOf<SomeClass<MyObject>>();
}
public async Task<IEnumerable<MyObject>> GetAllAsync(string SomeParameter)
{
var result = await repository.GetAsync(x => x.OtherParameter == $"{SomeParameter}.{nameof(MyObject)}s", default);
var reportDataItem = result.FirstOrDefault();
if (reportDataItem == null)
return null;
return reportDataItem.Collection;
}
Here is my test var data = await MockDBHelper.GetAirportsAsync(someParameter, true);
mockIRepository.Setup(x => x.GetAsync(null, default)).ReturnsAsync(data);
mockFactory.Setup(x => x.RepositoryOf<SomeClass<MyObject>>()).Returns(mockIRepository.Object);
_repo = new MyObjectRepository(mockFactory.Object);
var result = await _repo.GetAllAsync(AirlineCd);
mockRepository.Verify(x => x.GetAsync(null,default), Times.Once());
Upvotes: 1
Views: 1877
Reputation: 554
This statement
mockIRepository.Setup(x => x.GetAsync(null, default)).ReturnsAsync(data);
will only return data
when the values of null
and the default
value are passed as the two parameters. Since you are passing a function as the first parameter it is not null, therefore not matching the setup. Instead you could tell mock to "Match anything" for the function.
mockIRepository.Setup(x => x.GetAsync(It.IsAny<Func<PARAMETER_TYPE, RETURN_TYPE>>(), default)).ReturnsAsync(data);
You will need to replace PARAMETER_TYPE
and RETURN_TYPE
with the appropriate types used in your code.
Upvotes: 1