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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events