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

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

  • TypeScript Tops New JetBrains 'Language Promise Index'

    In its latest annual developer ecosystem report, JetBrains introduced a new "Language Promise Index" topped by Microsoft's TypeScript programming language.

Subscribe on YouTube