简体   繁体   English

如何以编程方式更改方法

[英]how to programmatically change a method

I am writing a test application to test an Operation Contract in the a service 我正在编写一个测试应用程序以测试服务中的运营合同

Test client: 测试客户:

here is how I am making a call from my test to get the returned bool value 这是我从测试中拨打电话以获取返回的bool值的方式

   bool operateResult =  TestContext.ServiceOperator.Operate(ref Inputmessage);

Service: 服务:

The operate method above calls another method 上面的操作方法调用了另一个方法

      bool  Operate(){
        OperatorProcess(msg, interface);
      }

        private bool OperatorProcess(ref Message MessageData, string SendInterface)
        {
        parse(message);
        validate(message);
        Transfer(message);
        }

From my test my goal is to test the operator method but in the process operator I don't want to reach the Transfer(message) I want it to exit right after validate and get the result. 从我的测试中,我的目标是测试操作员方法,但是在流程操作员中,我不想到达Transfer(message),我希望它在验证并获得结果后立即退出。

How can I programmatically modify it from my test application? 如何从测试应用程序中以编程方式对其进行修改? Thanks 谢谢

Note : the transfer method send the parsed and validated method to another service and I want to stop it from sending my request to that service. 注意:转移方法将经过解析和验证的方法发送到另一个服务,而我想阻止它将我的请求发送到该服务。

Why don't you try extending the class that contains the OperationProcess function and overriding the function then calling your overridden function that only does the few parts you care about... You can keep the parts in your function that you care about and only the functionality you need to test for. 为什么不尝试扩展包含OperationProcess函数的类并覆盖该函数,然后调用仅覆盖您关心的几个部分的覆盖函数...您可以将这些部分保留在您关心的函数中,而仅保留您需要测试的功能。

C# examples on how to override 有关如何覆盖的C#示例

You can declare a global boolean variable test then set it to true during testing and set it to false during production, then you method will look similar to 您可以声明一个全局布尔变量测试,然后在测试过程中将其设置为true,在生产过程中将其设置为false,然后您的方法将类似于

private bool OperatorProcess(ref Message MessageData, string SendInterface)
{
    parse(message);
    validate(message);
    if(!TEST)  Transfer(message);
}

Alternatively, if you don't want to create a global variable, then you can just add a parameter to the method signature 另外,如果您不想创建全局变量,则只需在方法签名中添加一个参数即可

private bool OperatorProcess(ref Message MessageData, string SendInterface, bool performTransfer)
{
    parse(message);
    validate(message);
    if(performTransfer)  Transfer(message);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM