I am trying to test async method but i am getting null reference exception.
I have a service that has a GetAll method which is async: public async Task<IQueryable<T>> GetAll()
and the method under test is calling this method: await _service.GetAll();
I am mocking the service and then doing an arrange on the getAll method, but i get a null reference exception when the method is called in the code under test.
Mock.Arrange(() => mockService.GetAll()).Returns(Task.FromResult<IQueryable<Models.VM>>(vms.AsQueryable()));
Thanks
Vikas Mittal
5 Answers, 1 is accepted
I don't see anything out-of-place with your snippet. Can you give us a full repro project so that we can investigate the issue?
Regards,
Stefan
Telerik
It is working now.
Thanks
vikas
Hi Vikas,
I got exactly the same issue with yours, do you mind to let me know how did you fix this issue?
Cheers,
Larry
I think I had to make 2 changes, make the unit test method async and use await to call the method under test.
[TestMethod]
public async Task GetVirtualMachinesTest(){
// Act
await ((Task)privViewModel.CallMethod("GetData", context));
}
Did you manage to fix the problem?
Regards,
Kamen Ivanov
Progress Telerik