09 November 2011

What Are Mole and Stub Types?


Moles generates two types, in the Moles assemblies: Mole types and Stub types.  Both behave very much the same, but are are used in mutually exclusive situations.  See page 3 of the Moles Reference Manual, for more information on stub and mole types.

An important concept often overlooked by developers investigating the Moles Framework is that the mole and Stub types may be configured with frequently-used detours, in a private, parameterized test class method.  This allows you to centralize the setting of frequently-used values, and therefore adhering to the DRY principle.

Stub Type

Stub types are generated for all interfaces and non-sealed classes containing virtual or overridable methods.  Even though a stub is generated, it can not be used for static or non-overridable methods of the targeted type.  These methods will simply be absent from the stub type.

These types may be used in the same circumstances as a normal stubs, especially dependency injection.  The benefit to using a moles-generated Stub type is the ability to circumvent the need to generate an entire stub for specific use.  Stub types contain a default implementation of its members, allowing you to detour only specific members, for a specific purpose.

NAMING

  • The Stub type prefixes the target type with an "S" character (e.g. "IFileSystem" becomes "SIFileSystem")
  • Members of the stub include the target member name, followed by the argument types, to disambiguate method overloads (e.g. "GetData(string productName, int quantity)" becomes "GetDataStringInt")*

Moles Type

Moles types are used in all situations where stub types are not allowed, including:
  • Static classes
  • Sealed classes
  • Private classes
  • Non-overridable methods
  • Static methods
Moles types are incredibly useful when testing with third-party APIs, legacy code, and .NET Framework dependencies.

NAMING

  • The Mole type prefixes the target type with an "M" character (e.g. "YourMethod" becomes "MYourMethod")
  • Members of the mole include the target member name, followed by the argument types, to disambiguate method overloads (e.g. "GetData(string productName, int quantity)" becomes "GetDataStringInt")*

*NOTE:  Generic collections are also named in the Stub and Mole type members.  For example, "GetData(string productName, List quantity)" becomes "GetDataStringListOfInt")


No comments:

Post a Comment

Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.