Unit test the delegate action is called

I have a dictionary that I use to avoid writing big if statements. It maps an enum to an action. It looks like this:

 var decisionMapper = new Dictionary<int, Action>
                             {
                                 {
                                     (int) ReviewStepType.StandardLetter,
                                     () =>  
                           caseDecisionService.ProcessSendStandardLetter(aCase)
                                     },
                                 {
                                     (int) ReviewStepType.LetterWithComment,
                                     () => 
                          caseDecisionService.ProcessSendStandardLetter(aCase)
                                     },
                                 {
                                     (int) ReviewStepType.BespokeLetter,
                                     () =>              
                          caseDecisionService.ProcessSendBespokeLetter(aCase)

                                     },
                                 {
                                     (int) ReviewStepType.AssignToCaseManager,
                                     () => 
                          caseDecisionService.ProcessContinueAsCase(aCase)
                                     },
                             };

      

then I call this in my method:

     decisionMapper[(int) reviewDecisionRequest.ReviewStepType]();

      

My question is how can I unit test these mappings? (I am using Nunit and C # 4.0)

How can I argue that when I call my decision method - that 1 is equal to the call to -caseDecisionService.ProcessSendStandardLetter (aCase).

Many thanks.

+3


source to share


3 answers


Thanks everyone for helping me with this. That was what I did at the end.

I poked fun at calling the action service, then calling the dictionary value, and then calling AssertWasCalled / AssertWasNotCalled. Like this:



        mapper[(int) ReviewStepType.StandardLetter].Invoke();
        caseDecisionService.AssertWasCalled(c => c.ProcessSendStandardLetter(aCase),
                                            options => options.IgnoreArguments());
        caseDecisionService.AssertWasNotCalled(c =>  
                                               c.ProcessSendBespokeLetter(aCase),
                                               options => options.IgnoreArguments());
        caseDecisionService.AssertWasNotCalled(c => 
                                               c.ProcessContinueAsCase(aCase),
                                               options => options.IgnoreArguments());

      

+1


source


You cannot compare anonymous delegates (see this link). To check a Method

delegate property Action

you need to think a bit. It must match the method of the MethodInfo

method caseDecisionService

to be called. For example (you can rewrite to use a function to make the code shorter):



MethodInfo methodToCall =
   decisionMapper[(int)ReviewStepType.StandardLetter].Method;

MethodInfo expectedMethod =
   typeof(CaseDecisionService).GetType().GetMethod("ProcessSendStandardLetter");

Assert.AreSame(expectedMethod, methodToCall);

      

+2


source


I personally would not write a unit test that directly checks which action is called in each case.

Assuming this dictionary is part of a larger system, I would write one test that goes through every dictionary action through any class that contains the dictionary. I want to check that my code is giving me the results that I expect (the result of a call ProcessSendStandardLetter()

or ProcessSendBespokeLetter()

, for example); I am less interested in how exactly this is done.

+1


source







All Articles