locked
Why are stubs preferred to detours

    Question

  • In your Moles tutorial, "Unit Testing with Microsoft Moles", you say that refactoring is preferred because it does not require runtime instrumentation, but don't specify why instrumentation should be avoided when possible.

    It appears to me that detours would be preferable simply because the code under test does not need to be changed.

    Can you elaborate on this?
    Tuesday, March 30, 2010 6:37 PM

Answers

  • The next version of the Moles Reference Manual will have this guiding information:

     

    Stub types and mole types build on different technologies. They carry different requirements and properties. The following list and table summarize different aspects that will help you determine whether to choose stub types or mole types.

    ·         Detour implementation. The mole types rely on runtime code rewriting, which is implemented by a CLR profiler. The stub types simply rely on virtual method dispatch.

    ·         Performance. The runtime code rewriting used by mole types introduces significant performance degradation in execution time. The stub types do not have this performance overhead and are as fast as virtual methods can go.

    ·         Static methods, sealed types. The stub types can only influence methods that can be overridden. Therefore, stub types cannot be used for static methods, non-virtual methods, sealed virtual methods, methods in sealed types, and so on.

    ·         Deployment. The mole types require a CLR profiler to be installed on the machine to execute the tests. This might be an issue if the build machine cannot be modified. The stub types are self-contained and will work properly as long as the Moles framework assemblies are present.

    ·         Interfaces and abstract methods. Stub types provide implementations of interfaces and abstract methods that can be used in testing. Mole types cannot instrument interfaces and abstract methods, because they do not have method bodies.

    In general, we recommend that you use stub types to isolate from dependencies. This can be achieved by hiding the components behind interfaces. Mole types can be used to isolate from third-party components that do not provide a testable API.

    Stub Types and Mole Types Comparison

    Feature

    Stub types

    Mole types

    Detour mechanism

    Virtual method overriding

    Runtime instrumentation

    Static methods, sealed types

    No

    Yes

    Performance

    Fast

    Slower

    Deployment

    Xcopy

    Installer

    Abstract methods

    Yes

    No



    Nikolai Tillmann
    • Marked as answer by casterle Thursday, April 01, 2010 5:13 PM
    • Marked as answer by casterle Thursday, April 01, 2010 5:13 PM
    Tuesday, March 30, 2010 10:49 PM
    Owner

All replies

  • The next version of the Moles Reference Manual will have this guiding information:

     

    Stub types and mole types build on different technologies. They carry different requirements and properties. The following list and table summarize different aspects that will help you determine whether to choose stub types or mole types.

    ·         Detour implementation. The mole types rely on runtime code rewriting, which is implemented by a CLR profiler. The stub types simply rely on virtual method dispatch.

    ·         Performance. The runtime code rewriting used by mole types introduces significant performance degradation in execution time. The stub types do not have this performance overhead and are as fast as virtual methods can go.

    ·         Static methods, sealed types. The stub types can only influence methods that can be overridden. Therefore, stub types cannot be used for static methods, non-virtual methods, sealed virtual methods, methods in sealed types, and so on.

    ·         Deployment. The mole types require a CLR profiler to be installed on the machine to execute the tests. This might be an issue if the build machine cannot be modified. The stub types are self-contained and will work properly as long as the Moles framework assemblies are present.

    ·         Interfaces and abstract methods. Stub types provide implementations of interfaces and abstract methods that can be used in testing. Mole types cannot instrument interfaces and abstract methods, because they do not have method bodies.

    In general, we recommend that you use stub types to isolate from dependencies. This can be achieved by hiding the components behind interfaces. Mole types can be used to isolate from third-party components that do not provide a testable API.

    Stub Types and Mole Types Comparison

    Feature

    Stub types

    Mole types

    Detour mechanism

    Virtual method overriding

    Runtime instrumentation

    Static methods, sealed types

    No

    Yes

    Performance

    Fast

    Slower

    Deployment

    Xcopy

    Installer

    Abstract methods

    Yes

    No



    Nikolai Tillmann
    • Marked as answer by casterle Thursday, April 01, 2010 5:13 PM
    • Marked as answer by casterle Thursday, April 01, 2010 5:13 PM
    Tuesday, March 30, 2010 10:49 PM
    Owner
  • Thanks, that's the information I was looking for.

     

    Thursday, April 01, 2010 5:12 PM