0

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);
        }
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
Raju Ahmed
  • 1,282
  • 5
  • 15
  • 24
  • 4
    "*didn't working.*" - is not a technical description of a problem, it shows no information about whether it compiles, what errors it throws, what your expectations are, or what happens – TheGeneral Feb 19 '20 at 06:28
  • i presume your application deadlocked and stuck when running the test. if so, please add `ConfigureAwait(false)` on your `return await result.ToListAsync()` and all the chains that uses `await` on the referenced assembly. see [this discussion](https://stackoverflow.com/a/13494570/4648586). – Bagus Tesa Feb 19 '20 at 06:32
  • here is error message ** 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](http://go.microsoft.com/fwlink/?LinkId=287068) .** – Raju Ahmed Feb 19 '20 at 06:37
  • Does this answer your question? [IDbAsyncEnumerable not implemented](https://stackoverflow.com/questions/26296091/idbasyncenumerable-not-implemented) – Bagus Tesa Feb 19 '20 at 06:51
  • Can u just replace your mock method like below and see what it says:_screenActionRepositoryMock .Setup(c => c.FindAll(It.IsAny>>())) .Returns(screenAction.AsQueryable()); – sharmav1 Feb 19 '20 at 06:56
  • @sharmav1 still getting the same error. In my manager at debugging time data is showing for `var z = result.ToList();` but it getting error for this line `return await result.ToListAsync();` – Raju Ahmed Feb 19 '20 at 07:58
  • please have a look at this post and see if it works for you: https://stackoverflow.com/questions/44947556/entity-framework-tolistasync-with-select – sharmav1 Feb 19 '20 at 09:34
  • Dear @sharmav1 still getting _The source IQueryable doesn't implement IDbAsyncEnumerable_ error. – Raju Ahmed Feb 19 '20 at 11:57
  • Dear @MichaelRandall Sir, can you help me, cause error is specific now. Please? – Raju Ahmed Feb 19 '20 at 12:00
  • can you please post what changes you have tried? – sharmav1 Feb 20 '20 at 09:34

0 Answers0