0

In my project we have created action based method like below...which work as expected in our project.

    public async Task MyMethod(Action<bool> SuccessAction, Action<Exception> ErrorAction)
    {
        try
        {
            SuccessAction(false);
        }
        catch (Exception ex)
        {
            ErrorAction(ex);
        }
    }

Now, for testing the above method below is how i have written the test method using NUnit.

    [Test]
    public async Task MyFirstTest()
    {            
       var myClass = new MyClass();
       await myClass.MyMethod(
            Result =>
            {
                Assert.IsTrue(Result);//as all are aware that this will throw an exception.
            },
            Error =>
            {                    
                Assert.Fail();
            });
    }

Now, my question is when ever there is an exception occured at MyFirstTest then that exception get caught at the MyMethod.

I am not sure why this is happening.

Can any one please provide an solution to handle this.

Please let me know if more information is required or my question is not clear.

aamankhaan
  • 491
  • 1
  • 9
  • 35
  • 1
    please provide an example code for the scenario you looking for. you provided a different scenario. the scenario you were provided is: exception in MyMethod -> caught in MyMethod -> caught in Task -> wrap the exception with aggregateexception -> the generated code throw the exception in MyFirstTest context – Old Fox Apr 10 '15 at 17:13
  • Please do provide more information about what you think is failing - and why you think it is failing – Stuart Apr 17 '15 at 12:18
  • @Stuart - This will fail because when your action based method returns false and try to assert.istrue with false value then the assert will throw an exception which will be caught at the action based method level which will then raise an erroraction...this is the scenario ur unit test will fail. (Thow the assert.istrue is correct but when u see the unit test result the assert value will be changed....My only concerns here is how to avoid error to caught on the method level which is been raised by unit test case method !!! – aamankhaan Apr 21 '15 at 08:15
  • why not just rewrite your test so that the callbacks set a value and then you test that value after the await has returned? – Stuart Apr 21 '15 at 09:04

0 Answers0