Moling 类型,保留特定方法的原始行为

发布于 2024-11-07 13:24:54 字数 349 浏览 0 评论 0原文

我正在使用 Microsoft Moles,并对以下主题感到好奇:

  • 有什么方法可以设置行为 某种类型(例如未实现)但是 保持a的原始行为 单一的具体方法?

我的目的是将要测试的方法与其类完全隔离(不知道从要测试的方法中调用的方法)。


以下代码对我不起作用,因为摩尔被完全禁用,并且任何使用的子方法都使用其原始行为:

MBaseObjectType.BehaveAsNotImplemented();
MolesContext.ExecuteWithoutMoles(() => mBaseObject.MethodToTest())

I'm using Microsoft Moles and am curious on the following topic:

  • Is there any way to set the behaviour
    of a type (eg. to unimplemented) but
    keep the original behaviour of a
    single specific method?

My intention is to completely isolate the method to test (without knowing the methods that are called from the method to test) from its class.

The following code doesn't work for me as moles is disabled completely and any used submethod uses its original behaviour:

MBaseObjectType.BehaveAsNotImplemented();
MolesContext.ExecuteWithoutMoles(() => mBaseObject.MethodToTest())

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

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

发布评论

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

评论(2

2024-11-14 13:24:54

我猜你只能使用存根来实现这种行为。

这是一个代码示例。我们将调用MethodToTestClassUnderTest的所有其他方法将抛出BehaviorNotImplementedException类型的异常。

public class ClassUnderTest
{
    public virtual void MethodToTest()
    {
        Debug.WriteLine("In MethodToTest");
        AnotherMethod();
    }
    public virtual void AnotherMethod()
    {
        Debug.WriteLine("In AnotherMethod");
    }
}

Moles 为我们生成一个名为 SClassUnderTest 的存根类,我们将用它来调用 MethodToTest

public void Test1()
{
    // A hack to call an overridden method.
    var mi = typeof(ClassUnderTest).GetMethod("MethodToTest");
    DynamicMethod dm = new DynamicMethod(
        "BaseMethodToTest",
        null,
        new Type[] { typeof(SClassUnderTest) },
        typeof(SClassUnderTest));
    ILGenerator gen = dm.GetILGenerator();
    gen.Emit(OpCodes.Ldarg_1);
    gen.Emit(OpCodes.Call, mi);
    gen.Emit(OpCodes.Ret);
    var baseMethodCall = (Action<SClassUnderTest>)dm.CreateDelegate(
        typeof(Action<SClassUnderTest>));

    // Arrange the stub.
    SClassUnderTest stub = new SClassUnderTest();
    stub.InstanceBehavior = BehavedBehaviors.NotImplemented;
    stub.MethodToTest01 = () =>
    {
        baseMethodCall(stub);
    };

    // Act.
    stub.MethodToTest();
}

I guess you can only use a stub to achieve this behavior.

Here is a code sample. We will call MethodToTest, all the other methods of ClassUnderTest will throw an exception of type BehaviorNotImplementedException.

public class ClassUnderTest
{
    public virtual void MethodToTest()
    {
        Debug.WriteLine("In MethodToTest");
        AnotherMethod();
    }
    public virtual void AnotherMethod()
    {
        Debug.WriteLine("In AnotherMethod");
    }
}

Moles generates a stub class for us called SClassUnderTest which we will use to call MethodToTest.

public void Test1()
{
    // A hack to call an overridden method.
    var mi = typeof(ClassUnderTest).GetMethod("MethodToTest");
    DynamicMethod dm = new DynamicMethod(
        "BaseMethodToTest",
        null,
        new Type[] { typeof(SClassUnderTest) },
        typeof(SClassUnderTest));
    ILGenerator gen = dm.GetILGenerator();
    gen.Emit(OpCodes.Ldarg_1);
    gen.Emit(OpCodes.Call, mi);
    gen.Emit(OpCodes.Ret);
    var baseMethodCall = (Action<SClassUnderTest>)dm.CreateDelegate(
        typeof(Action<SClassUnderTest>));

    // Arrange the stub.
    SClassUnderTest stub = new SClassUnderTest();
    stub.InstanceBehavior = BehavedBehaviors.NotImplemented;
    stub.MethodToTest01 = () =>
    {
        baseMethodCall(stub);
    };

    // Act.
    stub.MethodToTest();
}
梨涡少年 2024-11-14 13:24:54

可以为所有或特定的 Mole 实例保留原始行为。 Fallthrough 行为会禁用未注册方法的 Moles。

// All instances:
MFileWatcher.Behavior = MoleBehaviors.Fallthrough;

// Specific instance
var original = new BaseObjectType(null);
var moledInstance = new MBaseObjectType(original) { InstanceBehavior = MoleBehaviors.Fallthrough };

参考: http://social .msdn.microsoft.com/Forums/en-US/pex/thread/39af5a02-1cc9-4cf3-a254-3bdc923175db

The original behavior may be preserved for all or specific Mole instances. The Fallthrough behavior disables Moles for unregistered methods.

// All instances:
MFileWatcher.Behavior = MoleBehaviors.Fallthrough;

// Specific instance
var original = new BaseObjectType(null);
var moledInstance = new MBaseObjectType(original) { InstanceBehavior = MoleBehaviors.Fallthrough };

Reference: http://social.msdn.microsoft.com/Forums/en-US/pex/thread/39af5a02-1cc9-4cf3-a254-3bdc923175db

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