Arrange async methods

5 posts, 0 answers
  1. Vikas
    Vikas avatar
    2 posts
    Member since:
    May 2014

    Posted 07 Dec 2015 Link to this post

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



    Vikas Mittal

  2. Stefan
    Stefan avatar
    198 posts

    Posted 08 Dec 2015 Link to this post

    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?

    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
  3. Vikas
    Vikas avatar
    2 posts
    Member since:
    May 2014

    Posted 08 Dec 2015 in reply to Stefan Link to this post

    It is working now.




  4. Larry
    Larry avatar
    1 posts
    Member since:
    Oct 2013

    Posted 1 day and 22 hours ago in reply to Vikas Link to this post

    Hi Vikas,

    I got exactly the same issue with yours, do you mind to let me know how did you fix this issue?




  5. Vikas
    Vikas avatar
    37 posts
    Member since:
    Dec 2015

    Posted 1 day and 7 hours ago in reply to Larry Link to this post

    I think I had to make 2 changes, make the  unit test method async and use await to call the method under test.

            public async Task GetVirtualMachinesTest(){

    // Act
                await ((Task)privViewModel.CallMethod("GetData", context));



Back to Top