How and what should I be (unit) testing for in this method?

Posted by user460667 on Programmers See other posts from Programmers or by user460667
Published on 2012-03-31T12:09:43Z Indexed on 2012/03/31 17:43 UTC
Read the original article Hit count: 136

Filed under:
|
|

I am relatively new to unit testing and have a query about what/how I should be testing a certain method. For the following (psudo-c#) method I have created (not a real-life example) what would you test for?

Initially, my thoughts would be to test the output with variations on the dictionary of form fields, e.g. valid, invalid, missing values. However I also wonder how you would test to make sure the object values have been changed to the correct value and that the correct email message was attempted to be sent (obviously both services could/would be mocked).

I hope what I am asking makes sense, I appreciate this is a subjective question and the answers may be 'it depends' ;)

public bool ProcessInput(Dictionary<string, string> formFields, ObjService objService, EmailService emailService)
    {
        try
        {
                   // Get my object id
                   int objId;
                   if(!int.TryParse(formField["objId"], out objId)
                   {
                      return false;
                   }

                   // Update my object - would you validate the save against a DB or a mocked inmemory db?
                   var myObj = objService.Find(objId);
                   myObj.Name = formField["objName"];
                   objService.Save(myObj);

                   // Send an email - how would you test to make sure content, recipient, etc was correct? 
                   emailService.SendEmail(formField("email"), "Hello World");

                   return true;    
        }
        catch(Exception ex)
        {
        return false;
        }
    }

© Programmers or respective owner

Related posts about c#

Related posts about .NET