This is a migrated thread and some comments may be shown as answers.

Arrange async methods

5 Answers 843 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vikas
Top achievements
Rank 1
Vikas asked on 07 Dec 2015, 07:46 PM

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

Sort by
0
Stefan
Telerik team
answered on 08 Dec 2015, 12:20 PM
Hi Vikas,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Vikas
Top achievements
Rank 1
answered on 08 Dec 2015, 05:47 PM

It is working now.

 

Thanks

vikas

0
Larry
Top achievements
Rank 1
answered on 21 Jun 2017, 01:56 AM

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

0
Vikas
Top achievements
Rank 1
answered on 21 Jun 2017, 05:08 PM

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));

     

}

0
Kamen Ivanov
Telerik team
answered on 23 Jun 2017, 03:11 PM
Hi Larry,

Did you manage to fix the problem?

Regards,
Kamen Ivanov
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Vikas
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Vikas
Top achievements
Rank 1
Larry
Top achievements
Rank 1
Vikas
Top achievements
Rank 1
Kamen Ivanov
Telerik team
Share this question
or