如何用鼹鼠只调用一次代表一次/一次?

发布于 2024-11-28 06:24:30 字数 632 浏览 2 评论 0原文

怎么可能用鼹鼠只调用一次委托方法呢?

MyClass.AllInstances.ResultateGet = delegate { return new ResultatInfoCollection(); };

我只想调用方法“ResultateGet”一次,因为第一次在没有委托的情况下初始化非常复杂。

target.UpdateResultate(); //calls delegate "ResultateGet"
//Assert some stuff
target.Verify(); //needs original function "ResultateGet" so unit test is useful

我通常感兴趣的是如何在调用原始函数而不是委托之前调用一次摩尔委托或特定次数。

更新: 我找到了一个方法,看起来有点麻烦。还有更好的解决方案吗?

ResultatInfoCollection x = new ResultatInfoCollection();
MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);

How is it possible to call a delegated Method only once / one time with moles?

MyClass.AllInstances.ResultateGet = delegate { return new ResultatInfoCollection(); };

I want to call the Method "ResultateGet" only one time because the init is quite complex the first time without a delegate.

target.UpdateResultate(); //calls delegate "ResultateGet"
//Assert some stuff
target.Verify(); //needs original function "ResultateGet" so unit test is useful

I am generally interested how to call a moles delegate one time ore a specific number of times before the original function is called and not the delegate.

Update:
I found a way, that seems a little bit cumbersome. Any better Solution?

ResultatInfoCollection x = new ResultatInfoCollection();
MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-12-05 06:24:30

此外,请参阅我的回答:如何为“moled”方法从多个委托中分配/选择? 这提供了匿名内部门控逻辑的示例方法。

哦,好问题!我自己也遇到过这种情况。您正在寻找的称为“失败”行为(执行原始代码)。 Moles 绕道而行的匿名方法必须包含一个在第一次调用后失败的切换机制。不幸的是,目前我认为 Moles 中不包含失败功能。

您更新的解决方法正是您所需要的——调用fallthrough会做同样的事情。我建议添加一个哨兵值,doFallthrough,它可以控制调用:

bool doFallthrough = false;
ResultatInfoCollection x = new ResultatInfoCollection();
MyClass.AllInstances.ResultateGet = delegate { 
    if (!doFallthrough)
    {
        doFallthrough = true;
        return new ResultatInfoCollection();
    }
    MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);
};

调用特定次数只需要更改哨兵值类型:

int doFallthrough = 0;
ResultatInfoCollection x = new ResultatInfoCollection();
MyClass.AllInstances.ResultateGet = delegate { 
    if (++doFallthrough < 5)
        return new ResultatInfoCollection();
    MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);
};

Also, see my answer to: How to assign/opt from multiple delegates for a 'moled' method? This provides an example of gating logic inside the anonymous method.

Ooh, good question! I have encountered this, myself. What you are looking for is called a "fallthrough" behavior (execution of the original code). The anonymous method to which Moles detours must contain a switching mechanism that falls through, after the first call. Unfortunately, I don't believe a fallthrough feature is included in Moles, at this time.

Your updated workaround is exactly what you need -- calling fallthrough would do the same thing. I suggest adding a sentinel value, doFallthrough, that gates the calls:

bool doFallthrough = false;
ResultatInfoCollection x = new ResultatInfoCollection();
MyClass.AllInstances.ResultateGet = delegate { 
    if (!doFallthrough)
    {
        doFallthrough = true;
        return new ResultatInfoCollection();
    }
    MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);
};

Calling a specific number of times simply requires a change to the sentinel value type:

int doFallthrough = 0;
ResultatInfoCollection x = new ResultatInfoCollection();
MyClass.AllInstances.ResultateGet = delegate { 
    if (++doFallthrough < 5)
        return new ResultatInfoCollection();
    MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);
};
不打扰别人 2024-12-05 06:24:30

老问题,但由于我在搜索时发现了它,我会用我的解决方案为下一个人回答。

在大多数情况下,使用 MolesContext.ExecuteWithoutMoles 调用原始函数效果很好,但是,如果您从此调用下游迁移任何其他函数或类,它们也不会被迁移。

给定以下类:

public class TheClass
{
    public int TheFunction(int input){
        return input + TheOtherFunction();
    }

    public int TheOtherFunction(){
        return DateTime.Now.Minutes;
    }
}

如果您使用 MolesContext.ExecuteWithoutMoles 方法:

MTheClass.AllInstances.TheOtherFunctionInt = (instance) => {
    return 5;
};

MTheClass.AllInstances.TheFunctionInt = (instance, input) =>
{
    //do your stuff here, for example:
    Debug.WriteLine(input.ToString());

    var result = MolesContext.ExecuteWithoutMoles<int>(() => instance.TheFunction(input));

    //do more stuff, if desired    
    return result;
};

您的 OtherFunction 摩尔将不会被命中,因为它是在“无摩尔”范围内(间接)执行的。

但是,您可以随时添加和删除鼹鼠委托,以便您可以执行以下操作,如 鼹鼠文档(第 24 页)

MTheClass.AllInstances.TheOtherFunctionInt = (instance) => {
    return 5;
};

MolesDelegates.Func<TheClass, int, int> molesDelegate = null;
molesDelegate = (instance, input) =>
{
    //do your stuff here, for example:
    Debug.WriteLine(input.ToString());

    int result = 0;
    try{
        MTheClass.AllInstances.TheFunctionInt = null;
        result = instance.TheFunction(input);
    }
    finally{
        MTheClass.AllInstances.TheFunctionInt = molesDelegate;
    }

    //do more stuff, if desired

    return result;
};

MTheClass.AllInstances.TheFunctionInt = molesDelegate;

OtherFunction 鼹鼠仍然受到攻击。通过这种方法,您可以仅通过特定方法去除痣,而不会影响其他痣。我用过这个,而且有效。我看到的唯一麻烦是,如果您有递归函数,或者可能是多线程情况,它将无法工作。

Old question, but since I found it when I was searching, I'll answer it for the next person with my solution.

Using MolesContext.ExecuteWithoutMoles to call the original function works just fine in most cases, however, if you are moling any other functions or classes downstream from this call, they won't be moled, either.

Given the following class:

public class TheClass
{
    public int TheFunction(int input){
        return input + TheOtherFunction();
    }

    public int TheOtherFunction(){
        return DateTime.Now.Minutes;
    }
}

If you use the MolesContext.ExecuteWithoutMoles approach:

MTheClass.AllInstances.TheOtherFunctionInt = (instance) => {
    return 5;
};

MTheClass.AllInstances.TheFunctionInt = (instance, input) =>
{
    //do your stuff here, for example:
    Debug.WriteLine(input.ToString());

    var result = MolesContext.ExecuteWithoutMoles<int>(() => instance.TheFunction(input));

    //do more stuff, if desired    
    return result;
};

Your mole for OtherFunction will not be hit, because it was (indirectly) executed within the "without moles" scope.

However, you can add and remove moles delegates at any time, so that allows you to do the following, as outlined in the Moles Documentation (p. 24)

MTheClass.AllInstances.TheOtherFunctionInt = (instance) => {
    return 5;
};

MolesDelegates.Func<TheClass, int, int> molesDelegate = null;
molesDelegate = (instance, input) =>
{
    //do your stuff here, for example:
    Debug.WriteLine(input.ToString());

    int result = 0;
    try{
        MTheClass.AllInstances.TheFunctionInt = null;
        result = instance.TheFunction(input);
    }
    finally{
        MTheClass.AllInstances.TheFunctionInt = molesDelegate;
    }

    //do more stuff, if desired

    return result;
};

MTheClass.AllInstances.TheFunctionInt = molesDelegate;

The OtherFunction moles is still hit. With this method, you can remove moling just from the specific method without impacting your other moles. I've used this, and it works. The only trouble I can see is that it won't work if you have a recursive function, or possibly a multi-threaded situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文