Typemock Isolator 5.1 Released!

It’s time for another one. Usually we don’t two consequent major releases, but this one packs some punch. Most importantly, we now have static method support and non-public method faking.

So how do you do that?

Let’s look at this example:

[TestMethod]
[Isolated]
public void FakeStaticMethods_UseDefault()
{
Isolate.Fake.StaticMethods<LoggerFactory>();

Isolate.WhenCalled(() => LoggerFactory.Count).WillReturn(10);

Assert.AreEqual(10, LoggerFactory.Count);
}

The options for constructing a type with the StaticMethods API are the same for Fake.Instance<>, and you can read about creating fakes. And WhenCalled looks and works exactly as you’d expect. You can also use Fake.StaticConstructor<> to, well, you know.

Non-public member faking is also new in 5.1. It is string-based, and because of the fact you have properties methods and indexers, as well as static members you’d want to fake, it has a more complex API. I’ll be posting more on that, but for the meantime, let’s look at this example. Here, the Log method calls the IsDiskFull property, and based on the value it returns (which we fake) throws an OutOfDiskSpaceException:

[TestMethod]
[Isolated]
[ExpectedExcetion(typeof(OutOfDiskSpaceException))]
public void ChangePrivatePropertyReturnValue_SimulateDiskError()
{
RealLogger fake = Isolate.Fake.Instance<RealLogger>();

Isolate.NonPublic.Property.WhenGetCalled(fake, "IsDiskFull").WillReturn(true);

fake.Log("Hello World");
}

And of course, there’s the Verify counterpart (this time for an indexer):

[TestMethod]
[Isolated]
public void VerifyIndexGetterWasCalled()
{
LogFile fake = Isolate.Fake.Instance<LogFile>(Members.CallOriginal);

string line = fake.GetLineAt(10);

// Verify a get call to the LogFile's internal indexer was made
Isolate.Verify.NonPublic.Indexer.WasGetCalled(fake).WithIndex(10);
}

Suppose you have a real object, and want to fake a small part of its behavior. What you would do in 5.0 is go through Fake.Instance, like this:

LogFile fake = Isolate.Fake.Instance<LogFile>(Members.CallOriginal);

This was in order to use WhenCalled and Verify on that instance. In 5.1 we relaxed this requirement. You can just call new on it, and then just call WhenCalled on it. Same goes for static calls. For example, if we wanted a real object in the diskspace exception example before, and just change the private Get behavior, here’s how it looks:

[TestMethod]
[Isolated]
[ExpectedExcetion(typeof(OutOfDiskSpaceException))]
public void ChangePrivatePropertyReturnValue_SimulateDiskError()
{
RealLogger fake = new RealLogger();

Isolate.NonPublic.Property.WhenGetCalled(fake, "IsDiskFull").WillReturn(true);

fake.Log("Hello World");
}

Looks better, doesn’t it? Now there’s only one exception to the rule. If you want to Verify on an instance or type, you need to go through Fake or WhenCalled at least once. But WhenCalled always works.

What else do we have? A better RecursiveFake implementation, which is more stable than in 5.0. We cleared up a few error messages. We fixed an issue with returning a method returning a generic array. We also fixed a linking issue we had on 64 bit machines. And we now have, again, the integrated help installer to download.

We have a small breaking change: we changed Swap to SwapNextInstance, based on customer feedback. Reads clearer. And finally, we have improved our documentation.

Now, go ahead and download it.

  • Soon Hui

    Great! Can’t wait to install it.

    I am not sure whether mix parameter verification and event verification is supported in 5.1. That would be tremendously useful.

  • Gil Zilberfeld

    Let me know how it turned out!

  • AndrewSeven

    I’m using MSTest and I’ve created two tests that both isolate the static constructor.
    Isolate.Fake.StaticConstructor [MyContext]();

    Running both tests in the same run generates an error. In my current tests, this would normally be part of the test setup and not be an issue.
    Is there a way to unfake it or to detect that it is already faked?

  • Gil Zilberfeld

    You need to put the Fake.Static.Constructor in the [ClassInitialize] method, that is called only once.

    If you have tests that need the static constructor and tests that don’t, you need to separate them into different test projects. That’s because you cannot control when the cctor is invoked, and it only gets called once. That’s the only way to make sure that the the right tests invoke it correctly.

TOP