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

Expected Exception with async methods

7 Answers 88 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vikas
Top achievements
Rank 1
Vikas asked on 21 Jan 2016, 10:29 PM

I have a async method under test and this method calls a rest service asynchronously, I want to test that if the rest service call returns an error then this method under test throws it back, I am guessing because of the way the exception are wrapped in a Task it is not working for me.

If i debug I can see that service.restcall does throw the exception with "Test" but the test method never receives the error.

public async Task CreateDatastore()

{

await service.restcall()

}

 -------------- Test code -----------------

[TestMethod]
        [ExpectedException(typeof(Exception), "Test")] 

public void Should_Throw_Back_The_RestException()
        {
            //Arrange
            var service = Mock.Create<IRestApiServices>();
            var viewModel = new CreateViewModel(service);
            var privViewModel = new PrivateAccessor(viewModel);

            var ex = new Exception("Test");

            Mock.Arrange(() => service.restcall().IgnoreArguments().throws(ex).MustBeCalled();

            //Act            
            privViewModel.CallMethod("Methodundertest", null);

            //Assert
            Mock.Assert(service);
        }

7 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 26 Jan 2016, 07:10 AM
Hello Vikas,

You need to "await" all Tasks in the chain for the exception to be rethrown to the caller.

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 27 Jan 2016, 05:40 PM

Not sure I understand, I am awaiting on all the async calls in my real code. so this is my method under test. When I arrange this method and tell it throw an error and run my test the call to service.restcall() throws the error and it goes into the catch block.

What i am expecting is that if remove the try/catch then my unit test should get that same error.

Please note that the method under test is private method i am calling it from my unit test using private accessor

privViewModel.CallMethod("restcall", null);

Mock.Assert(service);

private async Task CreateDatastore()
{

try{
await service.restcall()

}catch (Exception ex){}
}

0
Stefan
Telerik team
answered on 29 Jan 2016, 11:35 AM
Hello Vikas,

Exceptions thrown in async methods are wrapped by the returned Task. So, I guess in your code you need to change to the following:
await privViewModel.CallMethod("CreateDatastore") as Task;


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 01 Feb 2016, 05:58 PM
I cannot do that callmethod is not a async method
0
Kaloyan
Telerik team
answered on 04 Feb 2016, 02:49 PM
Hi Vikas,

Here is a sample that works:
public class ClassWithNonPublicMembers
{
    private async Task<int> MePrivate()
    {
        return 1000;
    }
}
 
[TestMethod]
public async Task ShouldAwaitPrivateMethod()
{
    // ARRANGE
    var mockedClass = Mock.Create<ClassWithNonPublicMembers>(Behavior.CallOriginal);
 
    // ACT
    var inst = new PrivateAccessor(mockedClass);
    var actual = await ((Task<int>)inst.CallMethod("MePrivate"));
 
    // ASSERT
    Assert.AreEqual(1000, actual);
}

Please, note that it is mandatory for the test method to be an async Task as well.

Regards,
Kaloyan
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 Feb 2016, 04:35 PM

This works,

 

Thanks

0
Kaloyan
Telerik team
answered on 09 Feb 2016, 11:57 AM
Hello Vikas,

I am happy to hear that.

Please, let me know if you will require further assistance. We will be happy to help again.

Regards,
Kaloyan
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
Kaloyan
Telerik team
Share this question
or