chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male

Isolator AAA API – The Basics

What is AAA?

NUnit and likes have been using it for years. The Arrange, Act and Assert (AAA) structure of a test is very convenient, readable and maintainable. Using AAA a test looks like that:

Why Change?

One of the semantic problems in both Reflective and Natural Mocks, is that an expectation has two meanings: The first is what you expect to happen, and therefore should be verified. The second is behavior – what should happen when the method gets called – the method can be ignored ( or mocked or stubbed, depending on your dialect), called, throw, etc.

What happens when you want to just ignore a method? Well, you set an expectation and then calls RepeatAlways. What you actually say is “I want to set a behavior, but I don’t want to verify the call was made”. Because an expectation also includes behavior, you need to write two sort-of contradicting lines.

Well, no more. The AAA structure allows the separation. Here’s a simple test, using the new AAA format:

The Arrange part contains creation of instances and setting an expected behavior. In our case, WhenCalled() sets an Ignore behavior on the void Increment() method – basically stubbing the method. On the Act part, we exercise the code. That means, Increment() called, and based on the behavior we set, the call is ignored. Finally, in the Assert part, we’re verifying the method was called.
That’s it – slick, readable and expressive.

A Few Words About the Syntax

You’ve noticed by now we’re using the Lambda syntax in our APIs. That’s true. The new API is supported on .Net 3.5. But there are some minor changes from regular expressions (no pun intended): No matter what method you use, you don’t need to pass the parameters in the parenthesis.

In addition, the syntax is type-safe. Take a look at this:

image

The WillReturn clause identifies that fake.IntCall() returns an integer, and therefore will allow to enter only integer values.

Finally the default for WhenCalled is always – ignore, returning values, throwing exceptions. You don’t need to repeat the line for every occurance. The behavior for the method specified in WhenCalled is the default. And yes – we intend in the future to have repeats and conditional returns as supplements based on the method we’re calling. What we have for now is a simpler way to write tests.

 

Want to start unit testing? Take advantage of our 15-day FREE trial of Isolator Complete. Get it now