How To Use the Isolator New API in VS2005

So if the new AAA API comes in a new shiny 3.5 box, does it mean that it’s operable only in VS2008?

Well, No. It takes a few steps, and might not look as nice, but it’s possible, and pretty easy.

First you’ll need the .Net framework v3.5 installed on the machine. Can’t get around that. So if you have VS2008 installed on your machine, you have it. If you downloaded it as part of Windows Update, you have it. And if you don’t, you can download the latest SP for .Net 3.5.

Now that you have it installed, create a project in VS2005. The first thing you need to do (apart from the regular references), is to add a reference to System.Core:

image

Now that you have prepared mentally, it’s time to write some code. Obviously, VS2005 doesn’t support lambda expressions, but it’s a bit more delicate. Lambda expressions can be divided into two: The ones returning a value (which translate into a Func<T> where T is the returned type), and the ones that don’t (and translate into Action). Actions are pretty easy, and the anonymous delegate translates implicitly. However, the value-returning lambda expressions need an explicit cast.

Here’s an example. Let’s say, I have a form, which has a boolean property, called IsDirty. I can set its behavior like that:

MyForm form = new MyForm();

Isolate.WhenCalled((Func<bool>)
(delegate
{
return form.IsDirty;
}
)).WillReturn(false);

Notice I’m using the delegate syntax, since there’s no Lambda support, and the casting to Func<bool> since IsDirty is boolean. Another thing you’ll notice that because I use a delegate, I have to use

return

on the property, instead of just calling the property in regular lambda syntax.

Now if I have a void method DoSomething, it looks like that:

Isolate.WhenCalled(
(delegate
{
form.DoSomething("AAA");
}
)).IgnoreCall();

And here’s a Verify example:

Isolate.Verify.WasNotCalled(
(delegate
{
form.DoSomething("SSS");
}));

These ones are simpler, since there’s no casting involved.

  • While not exactly beautiful, using AAA syntax in VS2005 has these benefits:
  • You use Intellisense, write-time checks and and compile-time checks on the content of the delegate and available options.
  • You get to wear the “Lambda? It’s just an anonymous delegate!” T-Shirt (You’ll have to print it for yourself though, we’re out of stock :)).

So don’t fret if you don’t have the latest IDE. Old models still run fine.

  • ulu

    Great post!

    Being a VB-ee (and not ashamed of this:), I’d be happy to use the new syntax with VB. However, lambdas are implemented only halfway in VB: you can’t write a lambda for void methods. Can you do a similar trick as in your post, this time using a “regular” delegate (since VB doesn’t support anonymous delegates as well)?

    ulu

TOP