Arrange async methods

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

     

    Thanks

    Vikas Mittal

  2. Stefan
    Admin
    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?

    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
  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.

     

    Thanks

    vikas

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

    Posted 20 Jun 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?

     

    Cheers,

    Larry

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

    Posted 21 Jun 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.

    [TestMethod]
            public async Task GetVirtualMachinesTest(){

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

         

    }

  6. Kamen Ivanov
    Admin
    Kamen Ivanov avatar
    76 posts

    Posted 23 Jun Link to this post

    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
Back to Top