Product Reviews

Test-Driven Development with Typemock Isolator

Isolator .NET simplifies your unit testing by providing a flexible and convenient way to eliminate dependencies between objects.

A key issue in unit testing is determining what code actually failed. The solution is to replace calls to the "real" objects with calls to replacement code that's so simple it can't fail.

Typemock Isolator dramatically reduces the time required to set up these mocked scenarios. Adding three lines of Isolator code to your test will intercept that call and send it to an arbitrary method of your choosing. In that arbitrary method you can have a DataSet (or, perhaps, a List of Entity Framework objects) containing fixed values returned to the method under test. Those fixed values allow you to run your test and be confident that, should the test fail, the problem is with the method and not with the Data Access Layer (DAL) that's been bypassed or the data currently in the database.

As an example, this Isolator code, running under Visual Studio Test, first creates a mock DAL object and specifies that the next time a DAL is created this mock DAL should be used. It also ties the DAL's GetCustomersByRegion method to the output from a mock method called FixedCustomerValues. The test code then calls CustomersInRegionWithFreightCharges, which, presumably, calls the DAL's GetCustomersByRegion but will now get the results of the mocked method:

DAL mockDAL = Isolate.Fake.Instance<dal>();
Isolate.Swap.NextInstance<dal>().With(mockDAL);
Isolate.WhenCalled(() =>
DAL.GetCustomersByRegion("SP")).WillReturn(FixedCustomerValues());

CustomerFactorycf = newCustomerFactory();
List<Customer> res = cf.CustomersInRegionWithFreightCharges("SP");

Assert.AreEqual(res.Count, 6);

While this test follows the current Arrange, Act, Assert (or AAA) model for mocking, the Isolator API also supports the old Record/Replay and Reflective Mocks models.


[Click on image for larger view.]
Typemock Isolator Trace.Isolator includes a Trace tool that leverages the Isolator profiler-based architecture to report on what Isolator has done for you.

Profiler-Based Tool
Isolator is a profiler-based tool that intercepts method calls (other mocking frameworks generate replacement classes). This means that Isolator doesn't really care how the "real" object is constructed, so Isolator can work with static/Shared methods, sealed classes, objects created with the New keyword rather than the Isolator framework and legacy code (regardless of its inheritance structure) -- you can even mock private members. There's some overhead associated with running a profiler like Isolator, but you can disable Typemock from a menu added to your Visual Studio menu bar.

The Isolator package includes some other goodies, including a command-line runner, the TeamMate add-in that works with several profilers to report code coverage, a Lint tool that flags common problems in tests, and the IntelliTest add-in that provides support for generating tests as you write your code (Lint and IntelliTest are not available for Visual Basic tests).

I liked Typemock very much: I found the API syntax easy to use (great Help system) and the additional tools to be genuinely useful. But it's worth noting there are many free tools in this area: the venerable Rhino Mocks, Moq and FakeItEasy, among others. I don't think any of these tools provide all of the features and functionality that Typemock offers, but they might meet your needs and pocketbook.

Isolator .NET

Typemock Ltd.
Web:
typemock.com
Phone: 877-634-0165
Price: $799 for developer edition; $2,499 for build server license
Quick Facts: A mocking framework for isolating code under test from its dependencies
Pros: Easy to use with an extensive API that handles a variety of typical testing scenarios; several additional tools
Cons: Not all features available in Visual Basic



About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Semantic Kernel Agent Framework Graduates to Release Candidate

    With agentic AI now firmly established as a key component of modern software development, Microsoft graduated its Semantic Kernel Agent Framework to Release Candidate 1 status.

  • TypeScript 5.8 Improves Type Checking, Conditional Feature Delayed to 5.9

    Microsoft shipped TypeScript 5.8 with improved type checking in some scenarios, but thorny problems caused the dev team to delay related work to the next release.

  • Poisson Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demo of Poisson regression, where the goal is to predict a count of things arriving, such as the number of telephone calls received in a 10-minute interval at a call center. When your source data is close to mathematically Poisson distributed, Poisson regression is simple and effective.

  • Cloud-Focused .NET Aspire 9.1 Released

    Along with .NET 10 Preview 1, Microsoft released.NET Aspire 9.1, the latest update to its opinionated, cloud-ready stack for building resilient, observable, and configurable cloud-native applications with .NET.

  • Microsoft Ships First .NET 10 Preview

    Microsoft shipped .NET 10 Preview 1, introducing a raft of improvements and fixes across performance, libraries, and the developer experience.

Subscribe on YouTube

Upcoming Training Events