---
title: "Mocking On The Edge"
url: "https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/mocking-on-the-edge.md"
canonical: "https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/mocking-on-the-edge/"
tags: []
author: "Roy Osherove"
description: "Lesson 14 of 14 in The Art of Unit Testing & TDD Master Class in .NET (Bonus tracks on advanced patterns), 49:54. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Mocking On The Edge

1. [Courses](https://courses.osherove.com/#courses)
2. [The Art of Unit Testing & TDD Master Class in .NET](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/)
3. Bonus tracks on advanced patterns

Lesson 14 of 14

Duration: 49:54 [Watch on YouTube](https://www.youtube.com/watch?v=kUigkI9PM1o)

How far can you take a tool like typemock [Isolator](https://osherove.com/blog/2006/9/27/using-typemock-vs-design-for-testability.html "Using TypeMock vs. Design for testability (osherove.com)")? What wouldyou do with it?

Roy shows very advaced uses of Isolator and any [profiler based framework](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-6#ch06lev1sec1 "Constrained and unconstrained frameworks (The Art of Unit Testing, 2nd ed., ch. 6)").

## In this lesson

In a 400 level talk, Roy Osherove builds a domain specific [isolation framework](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-5#sigil_toc_id_99 "Defining isolation frameworks (The Art of Unit Testing, 3rd ed., ch. 5)") that [stubs](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-3#sigil_toc_id_62 "Reasons to use stubs (The Art of Unit Testing, 3rd ed., ch. 3)") out a third-party system by default, using the [Typemock](https://osherove.com/blog/2009/7/9/minimizing-unit-test-fragility-8-features-in-typemock-isolat.html "Minimizing unit Test Fragility – 8 features in Typemock Isolator to help (osherove.com)") Open AOP API, which hooks into the CLR profiler API. For SharePoint he [fakes](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-4#ch04lev1sec2 "The difference between mocks and stubs (The Art of Unit Testing, 2nd ed., ch. 4)") an SPSite title in one test, writes an interceptor that logs every method call, and uses [CThru aspects](https://osherove.com/blog/2009/2/25/adding-method-missing-support-to-vbnet-using-ctru-and-typemo.html "Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP (osherove.com)") in a custom test attribute to disable the SPSite constructor and skip SP global methods. He shows [SilverUnit](https://osherove.com/blog/2009/2/25/introducing-silverunit.html "Introducing SilverUnit (osherove.com)") using the same approach for Silverlight, unlike Microsoft's browser-based test framework, and ends with a song.

## Transcript

7,362 words

[\[00:16\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=16s) Okay, this is a 400 level talk and there are several assumptions which I'm going to open with but that first that you you've had experience with [unit testing](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_17 "Defining unit testing, step by step (The Art of Unit Testing, 3rd ed., ch. 1)"). This is not a talk about regular unit testing. This is about creating something else. How to intercept existence existing systems and create let's say a domain specific [isolation framework](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-5#ch05lev1sec1 "Why use isolation frameworks? (The Art of Unit Testing, 2nd ed., ch. 5)") for a system. So for example like an isolation system for Silverlight, an isolation system for SharePoint and so on.

[\[00:52\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=52s) And what I'm going to show here is step by step how I build a very simple system like that using Typemock open AOP which is an API that's exposed in the latest versions of the Typemock Isolator. I'm going to dig deep into code. So a lot of things like using Reflector delegates and lambdas and all that stuff I assume that you know. Let's see.

[\[01:23\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=83s) Just so you know the tool that I'm going to be using is called Typemock Isolator. It started out as an isolation framework and one of the things that I've designed into it is that the the a thing called open AOP APIs and for to understand what happens is that Typemock does not work just like any other framework. It actually hooks up to what's called a profiler API. All the CLR calls, all the calls running innet are created in IL compiled at runtime and executed.

[\[01:59\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=119s) But there are special set of APIs called profiler APIs which are just like sensors. You can connect them from unmanaged code and those sensors will let you do whatever you want and give you all the events in the system including the CLR before it compiles code during the compilation when it loads modules anything that you want. So you're basically it's like a rootkit if you think about it. It actually wraps the CLR and you can do whatever you want during the runtime of another application. If you've ever used profiling applications like trace and so on, they use those same APIs to give you all that information.

[\[02:34\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=154s) Isolator takes that approach and uses it to intercept those calls and return custom values and throw exceptions and change behavior. Those are the same APIs just used very differently. So what I'm going to show is how we've actually provided a managed hook into that API and how you can intercept calls throughout the system so that you would have what I call default [stubs](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec1 "Introducing stubs (The Art of Unit Testing, 2nd ed., ch. 3)") or default behavior from a system that by default works like in production.

[\[03:05\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=185s) Okay you can definitely go to my course about in August if you if you're interested just go to this URL. I think there's still like a one room left. Let's see. The next talk by the way is going to be about Silverlight unit testing and [SilverUnit](https://osherove.com/videos/2009/8/25/unit-testing-in-silverlight-silverunit-and-ms-sliverlight-te.html "Unit Testing in Silverlight - SilverUnit and MS Sliverlight Test Framework (osherove.com)") which is a unit test framework for Silverlight which I developed on top of the [CThru](https://osherove.com/blog/2009/2/25/adding-method-missing-support-to-vbnet-using-ctru-and-typemo.html "Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP (osherove.com)") engine which are you're going to see today. It's developed on top of those same APIs.

[\[03:36\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=216s) And what that framework allows you to do is to run Silverlight unit tests without needing a browser running only in memory using any framework whatsoever. It intercepts all the Silverlight related calls by default. You don't have to create [mocks or stubs](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-4#sigil_toc_id_79 "The importance of differentiating between mocks and stubs (The Art of Unit Testing, 3rd ed., ch. 4)") and all that stuff. You just use it like you use regular code. For anyone who's ever done here any Silverlight, you know that's basically impossible to do.

[\[04:07\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=247s) I've already talked about mocks and stubs. But I want to to imagine that you have this case where you have storage managers and manager class and you have an email sender and a log writer and you don't care about the log writer. You don't care about the email sender but you are in a system where log writer and email sender are part of this third party API that wraps your system. That is you never care about them. Sometimes you make calls to them. So you want to check that they're being called, but you always want to stub them out. There is no way where you don't want to stub them out because if you don't stub them out, you're going to have a problem in the system.

[\[04:40\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=280s) You're actually going to talk to some to something that you don't want to. Think SharePoint, think Exchange, think Active Directory. You want to separate to isolate the system to something that is more manageable under test. So what I'm proposing is that instead of what we did so far is in an ad hoc manner create mocks and stubs to simulate the email sender and the log writer and that those mocks and stubs went away after the test was finished.

[\[05:12\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=312s) What if we never have to create stubs for it ever again? What if the system by default is a big stub but my code doesn't realize it? The general rule with mocks and stubs is people usually say only stub or mock what you own. Here I'm going to say let's do the opposite. Let's stub out everything we don't own. Everything that's outside our code is going to be [fake](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-5#ch05lev1sec2 "Dynamically creating a fake object (The Art of Unit Testing, 2nd ed., ch. 5)").

[\[05:46\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=346s) Here's an example. Anyone here ever develop in SharePoint, by the way? Okay. Plenty of SharePoint developers here. I want to show you an example of a SharePoint related test. And for those of you who don't know the SharePoint API, SharePoint API is actually not that bad. It has a couple of interesting objects. One of them is called SPSite. You cannot run tests against SharePoint without having SharePoint installed on your machine. However, the tests that you're about to see are written against SharePoint.

[\[06:17\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=377s) I don't have SharePoint installed. All I have are a couple of SharePoint DLS. I am actually stubbing up the SharePoint DLS. First, I'm going to do it in an ad hoc manner. I'm just going to do it on the specific test and then I'm going to show how using the Typemock Open AOP APIs. Typemock open AOP API. Say that five times fast. I will stub out things by default so that everything I touch will actually be fake. Now I'm going to use Typemock Isolator because that's the only tool that can today fake out SharePoint.

[\[06:54\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=414s) Let's look at just at the name of the test for a second so we see what I'm trying to test. When you're trying to get a site title, given a specific URL, bring the site title. So, what I want to do is I want to to fake a site that has a specific title and a specific URL so that when the code goes to that URL, I want to see that it brings me that title. That's a very simple thing that you can do in SharePoint. Now the way I do it with the ad hoc testing with ad hoc APIs is by creating a fake instance of SPSite.

[\[07:24\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=444s) Isolate.fake.instance of SPSite. What's interesting is that SPSite by default is sealed. You cannot inherit from it. Doesn't implement any interface. And any other framework would not be able to do this. I create an a fake site and then I fake out the behavior fake site.root.title. Notice that there is an object with a property here. So there's like a chain of calls. We'll return

[\[07:56\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=476s) This title just a fake title. So this is a static this is sorry this is not a static property. This is just a call chain that will return something. Now the big point here is that I don't know when the SPSite will be created in production code. So I can swap out the next instance of SPSite. So isolate.swap.next instance of this type with the fake site with all the behavior that I just told you. Now the production code might look something like this.

[\[08:28\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=508s) New SPSite bring me the title and indeed this is what I will get. This is a very simple way to just isolate one specific thing in the system. Doesn't really mean anything. Now I want to try to do this on a piece of code that is one level removed. So this is I create a class called sprapper and sprapper is just think of it like a helper. I initialize it with a URL and it will have just the title already ready for me.

[\[09:02\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=542s) So I give it a URL and when I say get site title, it does all the things that I did before with a site. Notice that it also calls methods that I don't care about like dispose here and dispose there. And these are the kinds of methods that I shouldn't care about in my tests. These are like methods they say forget about it. Always ignore these methods. I shouldn't be needing to fake them out for every test that I have if this is the only domain I'm testing in.

[\[09:33\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=573s) Now, the same code that I did in the previous test will work even if I initialize the SP wrapper code. So, this there is no difference. I just I what I'm showing is that this will also work regardless of where the call to the new constructor will happen. If it's in my code, if it's some other code, if it's in third-party code, because we are using the profiler APIs, we get the notification that a call to constructor is called anywhere even if it's in third party code and we can in intercept that and return fake behavior.

[\[10:10\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=610s) So what now what I want to do is I want to create something called an interceptor. An interceptor is just a logical thing that I just thought about. It's something that uses the Typemock API open AOP stuff to intercept calls at runtime. Just intercept. So the first thing I want to do is just intercept and see all the calls that are running in the real system. I'm not going to actually do any testing. I just want to show you what goes on when I implement these two lines.

[\[10:43\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=643s) This interceptor I'm going to show the code. I just say start and stop and between them all the calls will be written to the console. Let's just see how that runs and then I'll show you how the code looks what it takes to do something like this. So just imagine that you just hooked up into the the the the APIs of the CLR and you get notification on every method that happens in the CLR from now on. These are all the methods that happen with the type names.

[\[11:14\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=674s) I could also write down the parameter values and all that stuff. As you can see, there are methods from Microsoft SharePoint here, but there's also a method from testdriven because that's the runner that I use because it's managed. The CLR also notifies me about this. So, these are things I didn't even care about. I also intercept. Let's see what else there is. There are calls from

[\[11:44\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=704s) Let's see is there like an NUnit kind of stuff here. Oh look at that system diagnostics.process.dispose I even cut that. So just imagine I can actually catch any call to the system as long as that method or type is not inside mscorlib.dll. It's not because I can't. because we chose not to do that because if you intercept mscorlib.dll DLL you get a very very bad runtime a slow slowdown

[\[12:17\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=737s) But anything that's not mscore DL we can intercept we can change behavior that means that we can intercept SharePoint we can intercept Silverlight anything this is the this is the constructor for the SharePoint wrapper and this is what the code looks like this is the simple interceptor the simple interceptor actually uses a class called event tunnel. Event tunnel is inside you get this basically when you download the the isolator type.in internal.hooks.event

[\[12:51\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=771s) Tunnel. You're not supposed to use this unless you're going to build some sort of framework. Now event tunnel has two main delegates that you can register to. These are not even events. These are delegates because we want you to be able to even nullify them when you want to. Now once you register to them they start working. Once you unregister from them that is you make them null they stop working. These are the two main hooks the two main sensors into the CLR.

[\[13:22\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=802s) Now there are two main things you can do here. There is one event that asks you if you would like to intercept a specific method. For example, it will give you the name of the method and the type name and all that kind of stuff. You say, okay, if this is a SharePoint method, I would say yes, I want to return result that will intercept. And if I did say yes, I would also get this. I would get a special delegate that invoked that's being invoked as the method is being called.

[\[13:55\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=835s) It will be called for any type of method. if it's a a static constructor, if it's a constructor and all that stuff, I will get this and it all the information I need is going to be inside the arguments parameter. For now, my implementation is very very simple. Let me just show you that implementation. okay. By default I always say args for for should intercept decision.

[\[14:30\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=870s) I can only say intercept or don't intercept or let isolator decide. That's the default. Sometimes you want isolator to decide for you when you don't care. By default, you don't care about something until you say I care about this. And the reason sometimes you want to say intercept is because maybe no one told isolator to do something with this method. So you say no no no I want to force I want to I want to be able to use this method by default I intercept everything I always say intercept.

[\[15:00\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=900s) Second thing I do for all method calls in the system I write to the console args.ype name and args method name but I also have the parameter values the generic parameters to the method and all that stuff so I can resolve everything essentially. These are the two hooks that isolator uses. So if you wanted to, you could actually use these two delegates to build your own Typemock Isolator with your own API with the same abilities. There is almost nothing you can do that that we do that you cannot do with this API

[\[15:33\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=933s) Except no I think that actually you can do everything once we do this we let we give you all the power that you want. Now when I say stop I just nullify them. as I nullify them, it will stop throwing the events which is good for performance. Of course, for every call in the system, I want I'm going to get very poor let's just intercept the calls that are happen on

[\[16:05\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=965s) SharePoint. This is how I would use it on a regular method call. And let's see this is SharePoint interceptor. Now with a SharePoint interceptor, what I did is I already changed my design. I created a class called interceptor base. It does the same thing you just saw. Registers to the two delegates, but it just has two methods that you need to implement. On method call and should intercept decision.

[\[16:35\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=995s) Why did I do a base class? Because maybe later I will want to do a different class that has different logic. So this is the same event tunnel stuff with two virtual methods. By default, it always says intercept and writes to the console. If I overwrite these methods, I can do whatever I want. So I inherit from this class and I say, look, on every method call, I want you to check if it's static. How do I know if a method call is static? The args has a property called context. That's the actual target object on which the method is performed.

[\[17:06\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1026s) For example, you can get the invocation of a constructor on an object before the object was initialized, but you actually get the instance of an object before a constructor. Okay? So, you get the instance of an object before the constructor happened. That means it's uninitialized, but you can still do a lot of stuff with it. For example, I can say for every object created in the system, if it inherits from a specific type, I want you to always fake a method called dispose.

[\[17:36\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1056s) Okay, because I intercept things at the system level, I now have the same behavior for all object in the system across the system. All future objects will behave the same. And I won't need to create special stubs for that to happen. And I'll show exactly how that happens in a second. I can just say for now, I'm just deciding if it's static, I'm writing it to the console. And if it's not static, I'm just writing it regularly. But this time I'm only intercepting if the rx.type name starts with Microsoft.sharepoint.

[\[18:08\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1088s) Only then do I intercept. Everything else will just go through. Let's see how I use this and what the output is. This is a regular SharePoint interceptor. I use the same two lines and I run this test. Now I should only be getting SharePoint related stuff. You can see there is a static call here and there is you know a lot of other thing.

[\[18:39\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1119s) Okay, ZoomIt died on me again. Come on just here we go and better. So we have a static method for SharePoint diagnostics there. You can see that we've discovered some really, really weird classes here. Some static stuff that happens inside SharePoint itself. Maybe we want to stop that out.

[\[19:11\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1151s) I can just go through all the call stack. And actually, when I built SilverUnit, this is exactly what I did. I tried to create an instance of the new Silverlight control. And I just wrote to the console everything. And then I saw where I got the first exception and I said, "Okay, I want to fake that exception out." So I caught a method with that name and I said ignore that method. I told isolator to ignore that method and I'll show you how to do that in a second. So these are static methods like SP utility CC tour. CCTur means it's it's a static constructor.

[\[19:43\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1183s) So I can disregard static constructors. I can disregard a lot of things. SP utility context download site get TLS all these stuff. So when you are at the system level, you are essentially putting your hands in the water and you're feeling everything that's flowing down there and you're going to see a lot of stuff you've never seen before. But that's exactly what we're doing with systemwide isolation. We are faking across the system. So we need access to everything. That's why the engine that is built on top of Typemock Open AOP, which I will show soon, it's called CThru.

[\[20:18\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1218s) You can see through the system, touch whatever you want. Let's see something a bit more elegant. This is a SharePoint interceptor that actually takes a URL and a fake title, but this time let's see if there's but this time it actually it inherits from something called aspect. There is this SPSite aspect class here

[\[20:51\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1251s) And it has some some class called aspect. What is this? This is nothing to do with Typemock AOP. This is already a project called CThru. CThru is an open source project which you can download from CodePlex. I wrote it. It's based on the open AOP stuff and it does everything I just showed but it already does all the hard work like overriding the methods and giving you the choice of what to return and what to do in a cleaner API. So let's say that I worked through all the methods that I want and I said, "Oh, you know what?

[\[21:23\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1283s) I have a specific aspect in the system." And the word aspect just means that I only care about this specific thing across the system. I have an aspect that only cares about a method called about a type called SPSite. When you inherit from aspect, you also can override the method that called should intercept. Typemock will call me and ask me, hey, do you want to intercept this? I will only return true when the type name is SPSite.

[\[21:55\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1315s) Why do I do this? Because this is so complex, I will create special aspects for special parts in the system and each one of them will have their own behavior. So I will inher inherit from I will create multiple classes and each one of them will take care of maybe a single type and will let it behave the way I want and I can override a bunch of several several other methods. Just so we can see the aspect class it has multiple things that I can inherit from.

[\[22:26\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1346s) I can override constructor behavior. I can override the specific method behavior. Missing method behavior is only good if you're in VB. You can actually create a missing method behavior like in Ruby using this kind of thing. Should intercept as of course you should already know this. You only tell it to intercept what you care about and you can intercept static constructors. The right method is just for logging. So I overrode constructor behavior for SPSite.

[\[22:57\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1377s) I always care about just constructors. Now the cute thing is I can use the Typemock open AOP and then when I get a specific call I can tell isolator to make a specific method behave in a specific way. So whenever someone calls a constructor on SPSite for every object in the system I tell isolator isolate.nonpublic non-public when called. So I want you to take over a non-public method of the object that's being created

[\[23:32\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1412s) And there is a method there called is valid web URL. How did I find this method? I opened Reflector and I looked at the code and I saw ah is valid URL returns false because I say fake. So I want it to always return true so that I can give it whatever URL I want because I'm crazy. So is valid URL will always return true. Now notice this happens in the constructor. That means that every object of type SPSite will never have an invalid URL.

[\[24:05\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1445s) Will never throw that exception if there was one. And make full URL will return the URL that I tell it to. What is the URL? I actually pass it into the aspect in the constructor. So I can control the URL of any SPSite that any method inside it will return. So now I can create an SPSite that when I ask what what is your address, what is your title, I can always control that. And

[\[24:35\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1475s) This is very very important. The arguments that are passed in allow me to change the behavior of the original method. And here I tell it to skip the actual method. So the actual method will never be called. So that means that the constructor will never actually be called the the the original one. Only the code that I do during the construction will happen, but all the other code inside the constructor will never execute.

[\[25:08\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1508s) So this way I just took a constructor of something that maybe has a lot of dependencies inside it and I shut it up. I said I don't care about this constructor. Shut it down. So every SPSite in the system will now have a constructor that's disabled. It will not call anything else. I created also another aspect of another class that I found called SP global. So I only care about type name that ends with SP global. And

[\[25:39\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1539s) Here's an interesting way to say something. All the methods in SP global will all be skipped. That's it. Imagine a system where you can say, you know what, I want to skip all the methods where you have more than one parameter. So you have this flexibility here. There is an SP global which I don't care about. I want calls internal calls to SP global which always throw exceptions because maybe it's starting a couple of threads underneath or whatever to always be shut down.

[\[26:10\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1570s) So that means always keep the actual method if the type name is SP global. So any call to SP global will not do anything. SP web this should be known to if you're doing SharePoint. Only intercept SP web stuff. And here what I'm going to do is to say when someone says get title I want you to return whatever title I give you in the constructor for the aspect. So now I can control the title for everything.

[\[26:40\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1600s) Now to initialize to use CThru I just added a reference. I created classes that inherit from aspect. And now I need to start the engine. I need to start this somehow. To do that there is a special class in the CThru project in the DL called CThru engine. I can add multiple aspects. I can even say add all the aspects in this assembly and I say add aspect SPSite with this URL. Add the aspects for SP web and add the SP global one.

[\[27:11\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1631s) So I took care of three things and I want you to start listening. And when I when they say stop, I just say stop listening and reset. And all this happens in a class called SharePoint interceptor. This is how it's used. I create a new SharePoint interceptor with this URL and this title. It will already initialize the aspect underneath with all the behavior that I said. And now I will actually be able to run the code and nothing will happen

[\[27:47\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1667s) Between the start and stop. These are the same two lines. And notice that also assert here has the fake title. So these are the same lines, but the constructors are disabled. The title always returns the same thing. Nothing really happened. I disabled a whole part of the system. It's like taking someone and cutting off one of their doing a labbotomy or something. But you only care about the public API of that system. Remember this is a third-party system. So you want to disable it.

[\[28:18\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1698s) The same thing was done with Silverlight. I disabled all the parts. It actually talk to the browser. If this is a method that belongs to a type that initializes stuff with JavaScript or reads XAML, disable it. Run everything just in memory. And then I had to recreate some of the behavior in memory. I'll show silver units near the end. Now, here's what I want to achieve in the end. I want to have my own attribute

[\[28:49\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1729s) SharePoint unit test. So, this is a SharePoint unit test where SharePoint the the SPSite with CNN.com has this fake title. So for this specific test, I will stub out the entire system, but the system will have these properties. So you'll notice this is like a custom domain language for for for SharePoint related tests. And this has the same behavior as I I I just showed. And the way you do it, let me just run this for a second.

[\[29:22\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1762s) I'm going to get another output. Where is the output window? Ctrl Alt O. See, I remember starting SharePoint isolation fake title. This is the title that I just wrote. Where did this come from? This is a custom attribute that you you get a special class that you can inherit from when you install Typemock Isolator. Let me show you how this works. When you get down to it, I've refactored the whole project into this.

[\[29:54\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1794s) There is a folder called aspects. There's SP global, SPSite and so on. This is the SharePoint interceptor which you've already seen the code to which initializes the CThru engine calls add aspect and so on. And this is the attribute. Now the attribute itself inherits from something called decorator attribute. This is built into isolator. It's always been there but most people didn't realize it was there. And what this allows you to do is to intercept a method before it's executed, do something before, and do something after.

[\[30:30\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1830s) And then you can execute the method in between. For example, in this case, I override the method called execute. And isolator will be responsible for calling me for invoking this method, the execute method. And here I initialize the SharePoint interceptor. I say start. I call the decorated method. This is the actual test method here. And you can do this today. And I say interceptor stopped.

[\[31:01\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1861s) And here is where I write to the console. Starting SharePoint isolation in Norway. So now you'll notice that the attribute has a URL and a title. It just uses them to call the interceptor itself. Let's see if I can show this. If I run this test again,

[\[31:32\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1892s) We should be able to see this. So this is just a simple way of creating your own attributes. And in fact, when we talk about SilverUnit, SilverUnit has its own attribute where you say it's a SilverUnit test. This is exactly what it does. Let me see if I can show you what SilverUnit looks like as I open things up. Anyone here work with Silverlight? Okay, I'll show you what a Silverlight unit test looks like and what happens when you when you try to run real Silverlight tests.

[\[32:12\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1932s) And what I what I want to show you what the framework looks like and you'll see that the framework is based on the same basic ideas of using the CThru engine and implementing aspects and all that stuff. It just takes it one place further. For those of you who've never seen this there is a Microsoft test framework for Silverlight and if you can and what you can do is you can create custom controls or things in Silverlight and then run them using

[\[32:44\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1964s) Microsoft Silverlight test framework. Now the test framework is an [integration test](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_25 "Integration tests (The Art of Unit Testing, 3rd ed., ch. 1)") framework. They call it a unit test framework but it's not. It's an integration test framework. It actually runs in the browser. And I want to show you first of all what my control looks like and how the tests for the control run in Microsoft and then what they look like in SilverUnit so you can see the differences. This is the control runner. I'll just going to run this for a second so you can see the control in action. Now as I run the control where is it?

[\[33:17\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=1997s) Let it run for a second. As you can see that I read my blog as I run. This is two instances of my control. I'm really into changing colors and stuff like that. If I if I click changes color if I put highlights here. So this is just control with some behavior. Right? If I do like this

[\[33:48\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2028s) Is better. Now, if I were to write tests against this control using Microsoft Silverlight test, and I'm not going to show too much, but this should be enough to explain to you that when I say it's not a unit test framework, it's really not a unit test framework. It's actually a test framework written in Silverlight. These are all the tests that I wrote for it with Microsoft Silverlight test. Runs in the browser. That means it runs slowly and it has a problem because sometimes the tests fail because of timing issues.

[\[34:20\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2060s) If the browser loses focus, you have a problem. Of course, you can't run it inside a build server. So, I decided to build SilverUnit, which using it, you can write tests that look kind of like this. Let me show you what tests look like, by the way, with Microsoft Silverlight framework mostly. This is what they look like. You write a test method and it's asynchronous, which is a custom attribute. And then you have to do everything asynchronously. For example, you want to add your control to a test panel, you have to incue it inside a method that takes an action.

[\[34:58\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2098s) And you have to incue test complete. And you have to remember to assert also using an inq call because they're all cued one after the other. They're all done asynchronously. And it sucks because the test is very very unreadable and I hated this really. It's a big problem. Not only that, it was really hard to get started with this framework. So the SilverUnit stuff looks like this. SilverUnit intercepted everything. And this is what kind of the same tests look like.

[\[35:36\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2136s) This is a regular test in SilverUnit. There is this attribute Silverlight unit test and this is SilverUnit setting template child and stuff like that inside the control which is part of the SilverUnit API. It's an API specifically for Silverlight. It's not an isolation framework. It's a domain specific isolation framework if you think about it and the asserts are also through SilverUnit. But there is nothing here that suggests asynchronicity or stuff like that. In fact, this is written using nunit.

[\[36:07\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2167s) I can run all the tests using testdriven. They will all run in the output window. I can run them in command line. And what it does is just allows you to test the output. Where is the output? These are the test results for the tests I just ran four. Of course, this runs in the command line. No problem. Now just as I close this out for a second, I want to show you that SilverUnit was actually written

[\[36:38\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2198s) In the in the same way that I just wrote the SharePoint stuff. SilverUnit has just a bunch of aspects that listen to a bunch of types. Most of the work creating SilverUnit was about debugging and understanding which APIs I want to take over. That's all. And about quarter of the work was about simulating various API events like how do you simulate a mouse click? How do you allow creating a control outside the browser?

[\[37:12\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2232s) Because a control by default loads XAML. By default, it loads threads and all that kind of stuff. So I need to understand how Silverlight works underneath. This is SilverUnit. It's actually written in VB.NET because I'm a VB developer at heart. I wrote it in VB.NET, but parts of it are written in C# just so other people won't feel, you know, neglected. It has a bunch of aspects. In fact, these are all the aspects.

[\[37:43\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2263s) They all inherit from an aspect in CThru engine. Just like you saw before, but they implement themselves on different types. For example, there is anything that inherits from control. There is an aspect anything that inherits from something called dependency object and so on. There is a dispatcher timer aspect. This is interesting because dispatcher timer is something that happens inside Silverlight controls. It's a real timer like thread related timer that I took over and I replace it with calls to a synchronous timer of my own.

[\[38:15\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2295s) So it runs without the need for the CLR Silverlight runtime. There are aspects where I don't care about them. I just implement always skip the method. Very very simple. Here's for example what a control aspect looks like. What else was it? If a control act aspect is always return true if the type name contains control and the method name is is enabled for example.

[\[38:46\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2326s) So I only intercept stuff if someone checks if a control is enabled in SilverUnit all controls are enabled by default. So I return I actually intercept that and I say return value or exception is true. The method behavior is return custom value and the return value is true. So controls are always enabled in SilverUnit. If you check control is enabled. By default it's enabled.

[\[39:17\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2357s) Why did I have to do that? because by default controls are disabled and you had a lot of framework stuff that was trying to check that enabled was true because that meant that the browser had loaded correctly. So I just implemented functionality where the browser always loads correctly. Dependency object is something that's interesting. I only intercept constructors or static constructors and for methods that start with add. And I'll this is just an example that I showed in a different has nothing to do with anything here.

[\[39:52\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2392s) Static constructor behavior is if this is a the static constructor for dependency object always skip. So no static constructors for the dependency object will ever be executed. It's a very simple concept. The constructor behavior is quite interesting. What I did here is I did look if you are getting the target type of the instance and you're checking that it equals a specific something I want you to skip the actual method but if it inherits from dependency object and it implements dispatcher I want you to implement it differently.

[\[40:32\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2432s) There are special behaviors based on the type. But I can say what what this means is that with this framework, I can say everything that implements this interface in the system will always have a property that returns this property that returns true. So I can check the subclass of something. The logic is very very simple because you have the type object of something. You have the parameter values. All you have to do is to say what the logic is and what value to return. or maybe you just want to throw an exception or whatever it is that you want.

[\[41:05\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2465s) So you have all the options of creating your own domain specific isolation framework. You just need to decide what APIs you want to intersect and all these APIs will be intercepted by default. There is no need from whoever is using your framework to to require something unless you specifically tell them to. That's basically SilverUnit. There isn't a lot of other things that you can do but I think that's quite enough.

[\[41:36\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2496s) There is I think we are going into this direction as stuff like SilverUnit and WPF and SharePoint and Active Directory are being you know we we're programming against these things and here we have this paradigm of unit testing and all that stuff but when we program against all these third party APIs we have only two options right now we either not unit test all the code that uses them or we abstract all those other systems away using a lot of other code.

[\[42:08\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2528s) This is a third way to solve the problem where instead of abstracting away, you actually create what what I call default stub or systemwide isolation so that you don't have to abstract the system. You use it as if it's the real system, but you don't care that it's actually just a stub. And I think I hope that people will build stuff on top of this that will allow us to check stuff like FxCop rules Active Directory WPF related stuff. You can actually take the SilverUnit stuff and adapt it to WPF very very easily.

[\[42:42\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2562s) A lot of the APIs are very much the same. I think it would be a very very simple thing to do. You know SharePoint of course is another thing. Everything that wraps our code in a different framework. Windows workflow. Can you think of Windows workflow activities in the workflow engine stubbed out by default? No need to create your own. No need to create anything else. Can you think of WCF that doesn't do anything? Anything in the system that you want to control, but you don't want to have to do it over and over again.

[\[43:13\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2593s) It's just there and doesn't do anything by default. That's what this type of system allows. Are there any questions? Okay. Who got everything I just said? Okay. Does anyone here use use WPF by the way by raise of hand or is just starting to look into WPF? Okay. You will see to WPF has the same set of issues that SilverUnit has and I hope that you start looking into that kind of thing.

[\[43:47\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2627s) We already have a couple of people that are starting to use SilverUnit in their own application. because they found the Microsoft test framework that it can't run in a build server that's really missing. Let's see. Okay, for further resources, you're welcome to go and download Typemock, download the a trial version and so on. Everything is built in. If you're building an open-source kind of thing, Typemock is free. It's free for open source projects. It's only not free if you're building a commercial thing on it.

[\[44:19\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2659s) The SilverUnit and CThru all the code can be downloaded for free. It's open source and it's based on Typemock which is not but just like you know Visual Studio plugins which are open source but Visual Studio is not open source. CThru.codeplex.com I'd love to hear what people would actually be doing with something like this. I think there are a lot of possibilities. Are there any questions before we move on to something else? Okay, I'll take that as an amazing no.

[\[44:52\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2692s) Huh? Okay. Would like to hear a little song then? Yeah. Okay. You're just waiting for the song up until now. Wow. You know, I'm not just a a piece of meat. You can treat me like with respect like like I have a brain. No, we just want to hear you sing, dude. Okay. Can I have a volunteer? >> There you go, sir. What's your name? And >> can you give a warm welcome to Andres, please. Thank you.

[\[45:23\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2723s) Do you know what you're going to get? >> You're going to get SilverUnit, a copy of SilverUnit for free. >> That's right. >> It's open source. You know that, right? Of course, you're going to get the book. Everyone gets a book today. It's my bribe to you. You know what to do. >> Okay. Can you then we won't give you the test. Can you press space once? Another time. One last time. That's it. Okay.

[\[46:02\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2762s) You probably know this. I've already done this in a different framework, different piece of whatever it was talk. It's been a long day. Let me just make sure something. Thought I had a different song here. No, it's not. It's the same song. Okay. Who heard this? Ah, crap. Okay. You want to sing with me then?

[\[46:36\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2796s) It's not like you don't know the words. You know what? Those who have never heard the song, the three people. This is a song that I wrote well not IRA I wrote about a team lead that I had that didn't want us to do agile development and they kept stopping us.

[\[47:06\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2826s) Every build you break, refactoring you make, every mock you fake, you little snake, I'll be watching you. Every standup meeting, I believe you're cheating. Every unit test pains my chest. I'll be watching you.

[\[47:43\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2863s) Oh, can't you see? This is scaring me. It would be so strange Now the clients hear everything they hear. Man, it bothers me.

[\[48:19\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2899s) Oh, can't you see? Nothing comes for free. Every test you write is a waste of ice. Oh, that's hard. Every build you break, every mark you think, every test you make, I'll be watching you. Every bill you break, every mark you fake, every test you make, I'll be

[\[48:54\]](https://www.youtube.com/watch?v=kUigkI9PM1o&t=2934s) Thank you very much. Okay, you are free. You have a bunch of extra five minutes, I think. Right. Thank you for pointing that out, by the way. Yeah, you're it's very well that I have a good sense of timing.

## Related

- [Adding Method-Missing support to VB.NET using CTru and Typemock Open-AOP](https://osherove.com/blog/2009/2/25/adding-method-missing-support-to-vbnet-using-ctru-and-typemo.html) · osherove.com

    Shows another use of the CThru engine and Typemock Open-AOP, the interception tools this talk builds on.

- [Digging deeper into isolation frameworks](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-6) · The Art of Unit Testing, 2nd ed., ch. 6

    The 2nd edition's chapter on how unconstrained, profiler-based frameworks work, the mechanism behind this talk's tricks.

- [Configuration via Typemock Interception](https://osherove.com/blog/2008/7/15/configuration-via-typemock-interception.html) · osherove.com

    Roy uses Typemock interception for something other than faking, the same kind of creative use this talk explores.

- [Introducing SilverUnit](https://osherove.com/blog/2009/2/25/introducing-silverunit.html) · osherove.com

    Introduces SilverUnit, the Silverlight framework the talk shows as the same approach applied to another platform.

### Also linked in this lesson

#### The Art of Unit Testing, 3rd ed.

- [Defining isolation frameworks](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-5#sigil_toc_id_99), ch. 5
- [Reasons to use stubs](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-3#sigil_toc_id_62), ch. 3
- [Defining unit testing, step by step](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_17), ch. 1
- [The importance of differentiating between mocks and stubs](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-4#sigil_toc_id_79), ch. 4
- [Integration tests](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_25), ch. 1

#### The Art of Unit Testing, 2nd ed.

- [The difference between mocks and stubs](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-4#ch04lev1sec2), ch. 4
- [Constrained and unconstrained frameworks](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-6#ch06lev1sec1), ch. 6
- [Why use isolation frameworks?](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-5#ch05lev1sec1), ch. 5
- [Introducing stubs](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec1), ch. 3
- [Dynamically creating a fake object](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-5#ch05lev1sec2), ch. 5

#### osherove.com

- [Minimizing unit Test Fragility – 8 features in Typemock Isolator to help](https://osherove.com/blog/2009/7/9/minimizing-unit-test-fragility-8-features-in-typemock-isolat.html)
- [Using TypeMock vs. Design for testability](https://osherove.com/blog/2006/9/27/using-typemock-vs-design-for-testability.html)
- [Unit Testing in Silverlight - SilverUnit and MS Sliverlight Test Framework](https://osherove.com/videos/2009/8/25/unit-testing-in-silverlight-silverunit-and-ms-sliverlight-te.html)

[Previous lesson Testing Complex Validation Knowledge using Extract & Override](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/testing-complex-validation-knowledge-using-extract-and-override/) [Last lesson Back to the course overview](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
