How Moles Isolation framework is implemented?
Asked Answered
V

3

23

Moles is an isolation framework created by Microsoft. A cool feature of Moles is that it can "mock" static/non-virtual methods and sealed classes (which is not possible with frameworks like Moq). Below is the quick demonstration of what Moles can do:

Assert.AreNotEqual(new DateTime(2012, 1, 1), DateTime.Now);

// MDateTime is part of Moles; the below will "override" DateTime.Now's behavior
MDateTime.NowGet = () => new DateTime(2012, 1, 1); 
Assert.AreEqual(new DateTime(2012, 1, 1), DateTime.Now);

Seems like Moles is able to modify the CIL body of things like DateTime.Now at runtime. Since Moles isn't open-source, I'm curious to know which mechanism Moles uses in order to modify methods' CIL at runtime. Can anyone shed any light?

Vyatka answered 11/6, 2010 at 9:52 Comment(1)
I highly recommend watching these 3 videos of @Peli explaining how Pex and Moles work. They are very good! Video Part 1; Video Part 2; Video Part 3Salverform
P
51

Moles implements a CLR profiler (in particular the ICorProfilerCallback interface) that allows to rewrite MSIL method bodies before they are compiled into assembly code by the .NET runtime. This is done in particular through the JitCompileStarted callback.

In each method, Moles introduces a detour that looks like this:

static struct DateTime 
{
    static DateTime Now
    {
        get 
        {
            Func<DateTime> d = __Detours.GetDelegate(
                null, // this point null in static methods
                methodof(here) // current method token
                );
            if(d != null)
                return d();
            ... // original body
        }
    }
}

When you set a mole, your delegate is stored in the underlying __Detours dictionary which gets looked up whenver the method is executed.

Paranoiac answered 18/6, 2010 at 8:42 Comment(3)
+1 for actually expaining HOW That works. I was curious, too. Nice to know there actually IS a way to rewrite a method in debug mode ;)Rearward
+1 from me as well - I was wondering how they did it and chanced upon this - makes sense as well.Habakkuk
I highly recommend watching these 3 videos of @Paranoiac explaining how Pex and Moles work. They are very good! Video Part 1; Video Part 2; Video Part 3Salverform
M
0

This is working like as wrapper to any assembly you want, for example mscorlib (this example baseing on Moles Assembly Wrapper of mscorlib). This giving to you power to replace any .NET method by the delegate written by coder.

This is not working automagicaly. You must first create before this start works, Moles XML configuration file with list of Assemblies to "Wrapper" and by this code Moles generate a References of this assembiles from config file. And you must in this file add using namespace System.Moles, and before function [HostType("Moles")]

Maim answered 11/6, 2010 at 9:56 Comment(0)
I
0

There is a presentation titled "Moles: Tool-Assisted Environment Isolation With Closures". Note the ending, "WITH CLOSURES". It was written in 2010. One of the authors was Jonathan de Halleux. I will assume that this is the formal first name of Peli de Halleux. This paper can be found in the book "Objects, Models, Components, Patterns: 48th International Conference, TOOLS 2010, Málaga, Spain, June 28 - July 2, 2010, Proceedings". Here is a link to sample pages of the article :

https://www.google.com/books/edition/Objects_Models_Components_Patterns/hvzmnCtNffUC?hl=en&gbpv=1&dq=moles+halleux&pg=PA253

Here is a link to the book :

https://www.google.com/books/edition/Objects_Models_Components_Patterns/hvzmnCtNffUC?hl=en&gbpv=1&dq=Objects,+Models,+Components&printsec=frontcover

Pex and Moles are mentioned in "Practical Microsoft Visual Studio 2015", "Real World .NET, C#, and Silverlight", "Agile Software Engineering with Visual Studio", "Pro .NET Best Practices". See "Pex and Moles - Isolation and White box Unit Testing for .NET" :

http://research.micvrosoft.com/projects/pex/

The latest URLs on Archive.org for the 3 videos are :

https://web.archive.org/web/20210917122427/http://channel9.msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-13

https://web.archive.org/web/20210917131545/http://channel9.msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-23

https://web.archive.org/web/20210917141034/http://channel9.msdn.com/Blogs/channel9spain/Microsoft-PEXMOLES--advanced-Unit-Testing-aspects-33

More videos with Peli de Halleux :

https://web.archive.org/web/20210514205423/https://channel9.msdn.com/Shows/Going+Deep/Nikolai-Tillman-and-Peli-de-Halleux-Inside-Code-Digger

https://web.archive.org/web/20210624042526/https://channel9.msdn.com/Blogs/matthijs/Pex-Unit-Testing-of-SharePoint-Services-that-Rocks

Impostor answered 13/5, 2023 at 18:58 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Funiculate
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewCleanly

© 2022 - 2024 — McMap. All rights reserved.