Unit Testing Void method that calls another void runs Task ()

I'm looking for some advice on writing some unit tests for the code below. Execution aside (this is not my code, but I was tasked to write some tests for it retroactively) can anyone suggest how I can test this? I am not using nUnit or a similar framework; I am using the testing tools built in Visual Studio.

I'm new to writing unit tests, but I think I should at least test the following:

  • Valid response passed to method SaveFormBrokerResponse()

  • Check for valid exceptions thrown catch()

  • Testing running Task

    but not sure how to do it

I got rid of this function a bit, it mainly has to do with the instantiation and population of some objects:

public void SaveResponse(IForm form, bool isLive, HttpRequestBase request)
{
    try
    {
        var response = new FormBrokerResponses();
        // Initialize some vars on response

        using (var memory = new MemoryStream())
        {
            var serializer = new DataContractSerializer(typeof(FormKeyValue[]));
            serializer.WriteObject(memory, request.Form.AllKeys.Select(r => new FormKeyValue(r, request.Form[r])).ToArray());
            memory.Flush();
            memory.Seek(0, SeekOrigin.Begin);
            response.Values = Encoding.UTF8.GetString(memory.ToArray());
        }

        _dataHandler.SaveFormBrokerResponses(response);
    }
    catch (Exception ex)
    {
        throw new Exception("boom explosions");
    }

    Task.Factory.StartNew(() => DispatchFormResponseViaEmail(form, isLive, request.Form.AllKeys.ToDictionary(r => r, r => (object)request.Form[r])));
}

      

I understand that testing void

is complex and questionable and that there are some integration testing issues here, but I said that I could not (currently) change the implementation and write tests for what I have.

+3


source to share


2 answers


If you are allowed to make any changes to the code, I would do the following, it's just a small change:

public void SaveResponse(IForm form, bool isLive, HttpRequestBase request)
{
    try
    {
        var response = new FormBrokerResponses();
        // Initialize some vars on response

        using (var memory = new MemoryStream())
        {
            var serializer = new DataContractSerializer(typeof(FormKeyValue[]));
            serializer.WriteObject(memory, request.Form.AllKeys.Select(r => new FormKeyValue(r, request.Form[r])).ToArray());
            memory.Flush();
            memory.Seek(0, SeekOrigin.Begin);
            response.Values = Encoding.UTF8.GetString(memory.ToArray());
        }

        _dataHandler.SaveFormBrokerResponses(response);
    }
    catch (Exception ex)
    {
        throw new Exception("boom explosions");
    }

    Dispatch(form,isLive,request);
}

virtual void Dispatch(IForm form, bool isLive, HttpRequestBase request){
    Task.Factory.StartNew(() => DispatchFormResponseViaEmail(form, isLive, request.Form.AllKeys.ToDictionary(r => r, r => (object)request.Form[r])));
}

      

I don't know what this class is called, so supposing the class is called DutClass, now you can get another implementation of this class like this:



public class UnitTestClass : DutClass{
    override Dispatch(){
        //don't do anything or set a state variable that this method was called
    }
}

      

Then, instead of testing the DutClass, you test the UnitTextClass, which has a different dispatch method implementation and doesn't fire the Task at all. Then you can check that this method was actually called, check for exceptions, etc.

+3


source


You can not. You have created a method that starts an asynchronous operation and then does not provide any means to monitor the completion / results of that operation for the caller. There are many ways to do this (returning a task, accepting a callback, an event, etc.), but you need to do something so that the caller can observe the results of the asynchronous operation. If the method does not expose anything, then there is nothing the caller can do.



+4


source







All Articles