Good day,
I'm new in Unit Testing using xUnit and Moq Framework in C#.
I'm trying to test an method wherein it returns list the method is responsible for returning list of info from Queryable method inside the repository class.
Here's my test method.
[Fact]
public void SelectInfoByName_InfoHasValue_ReturnInfoSelect()
{
var service = new Mock<ISearchInfoRepository>();
var selectInfo = new SelectInfoService(null, service.Object);
service.Setup(s => s.SearchInfoByName("info")).Returns(new List<Info>
{
new Info{ Name = "name1",InfoId = 1},
new Info{Name = "name2",InfoId = 2}
}.AsQueryable);
var expectedResult = new List<Info>
{
new Info{Name = "name1", InfoId = 1},
new Info{Name = "name2", InfoId = 2}
};
var result = selectInfo.SelectInfoByName("info").Result;
Assert.Equal(expectedResult, result);
}
Here's my SelectInfoByName responsible for returning the list of info by name
public async Task<IEnumerable<SearchSelect>> SelectInfoByName(string info)
{
var infoByName = searchInfoRepo.SearchInfoByName(info);
return await infoByName.Select(info => new SearchSelect
{
text = info.Name,
value = info.InfoId
}).ToListAsync();
}
Lastly, here's my repository or storage class where it communicates with the database using EF.
// storage or repo class
public IQueryable<Info> SearchInfoByName(string info)
{
return infoRepo.Info().Where(info => info.Name.Contains(name.Trim().ToLower()));
}
Note: Change from .AsyncState to .Result but still, the actual value is null
Thank you in advance.