Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用单元测试委托操作_C#_C# 4.0_Delegates_Nunit - Fatal编程技术网

C# 调用单元测试委托操作

C# 调用单元测试委托操作,c#,c#-4.0,delegates,nunit,C#,C# 4.0,Delegates,Nunit,我有一本字典,我用它来避免写大的if语句。它将枚举映射到操作。看起来是这样的: var decisionMapper = new Dictionary<int, Action> { { (int) ReviewStepType.StandardLetter,

我有一本字典,我用它来避免写大的if语句。它将枚举映射到操作。看起来是这样的:

 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)
                                     },
                             };
我的问题是如何对这些映射进行单元测试? (我正在使用Nunit和c#4.0)

当我调用decisionMapper时,如何断言1等于call-caseDecisionService.ProcessSendStandardLetter(aCase)


非常感谢。

您无法比较匿名代理(请参阅链接)。您必须使用一点反射来检查
操作
委托的
方法
属性。它必须匹配应调用的
caseDecisionService
方法的
MethodInfo
。例如(您可以重写以使用函数缩短代码):


我个人不会费心编写一个单元测试来直接检查在每种情况下调用的操作


假设这个字典是一个更大系统的一部分,我将编写一个测试,通过包含字典的任何类来完成每个字典操作。我想检查我的代码是否提供了我期望的结果(例如,调用
ProcessSendStandardLetter()
ProcessSendBespokeLetter()
的结果);我对检查它到底是如何工作的不太感兴趣。

谢谢大家的帮助。这就是我最后所做的

我模拟了操作服务调用,然后调用字典的值,然后调用AssertWasCall/AssertWasNotCall。像这样:

        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());

ReviewStepType是什么类型?枚举?如果您可以枚举.GetValues()(然后将每个值强制转换为int)。您好,谢谢,是的,这是一个枚举。这与测试有什么关系?我想知道我的映射是否正确。谢谢。尽管“.Method”返回映射器初始化时使用的服务方法的methodinfo。不是特定的操作方法信息。所以这个断言现在总是错误的。谢谢大家的帮助。我终于让它像这样工作了。我模拟了操作(caseDecisionService),在字典上调用Invoke,然后使用AssertWasCall/AssertWasNotCall。如下所示:映射器[(int)ReviewStepType.StandardLetter].Invoke();caseDecisionService.AssertWasCalled(c=>c.ProcessSendStandardLetter(aCase),options=>options.IgnoreArguments());caseDecisionService.AssertWasNotCalled(c=>c.ProcessSendBespokeRpcLetter(aCase),options=>options.IgnoreArguments());好主意,将其作为答案发布!
MethodInfo methodToCall =
   decisionMapper[(int)ReviewStepType.StandardLetter].Method;

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

Assert.AreSame(expectedMethod, methodToCall);
        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());