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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube