My project is using mvc5 in repository pattern and now i've a method in my ScreenManager. Now I've to write test code for a method which is async type and inside it uses linq query on my repository class. I already wrote a test code(see in bellow) for it but it didn't working. Please help me to get out of this problem.
Error:
Test Failed - ActionHistoryAsync_WhenCalled_ReturnSceeningActionViewModel Message: Test method ScreenMangerTests.ActionHistoryAsync_WhenCalled_ReturnScreenActionViewModel threw exception: System.InvalidOperationExcetpion: The source IQueryable doesn't implement. IDbAsyncEnumerable.Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068
I've a repository named
private readonly IScreenActionRepository _iScreenActionRepository;
And I'm Passing it through my constructor.
public ScreenManager(IScreenActionRepository iScreenActionRepository)
{
_iScreenActionRepository= iScreenActionRepository;
}
And finally my Executable method is following
public async Task<IEnumerable<ScreenActionViewModel>> ActionHistoryAsync(long screenId)
{
var result = from a in _iScreenActionRepository.FindAll(s => s.ScreenId == screenId && s.ScreenType == "T")
select new ScreenActionViewModel
{
Id = a.Id,
ScreenId = a.ScreeningId,
ScreeningType = a.ScreenType,
Description = a.Description,
ActionDateTime = a.ActionDateTime,
ActionBy = a.ActionBy,
RoleName = a.RoleName,
};
var z = result.ToList();
return await result.ToListAsync();
}
And my testing code is following
[DataTestMethod]
public async Task ActionHistoryAsync_WhenCalled_ReturnScreenActionViewModel()
{
Mock<IScreenActionRepository> _screenActionRepositoryMock= new Mock<IScreenActionRepository>();
long screenId = 1;
var screenAction = new List<ScreenAction>
{
new ScreenAction
{
ScreenId = 1,
ScreenType = "TP",
Description = "",
ActionDateTime = DateTime.Now,
ActionBy = 3,
RoleName = "Investigator"
}
};
_screenActionRepositoryMock
.Setup(c => c.FindAll(It.IsAny<Expression<Func<ScreenAction, bool>>>()))
.Returns(new Func<Expression<Func<ScreenAction, bool>>, IQueryable<ScreenAction>>(
expr => screenAction.Where(expr.Compile()).AsQueryable()));
//Act
var result = await _manager.ActionHistoryAsync(screenId);
//Assert
Assert.AreEqual(result.First().ActionBy, 3);
Assert.AreEqual(result.First().RoleName, "Investigator");
Assert.AreEqual(result.First().UserFullName, userList.First().FullName);
}