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:

[TestMethod]
public void SimpleTest()
{
// Arrange
RealLogger logger = new RealLogger();
logger.Count = 0;

// Act
logger.Increment();

// Assert
Assert.AreEqual(1, logger.Count);
}

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:

[TestMethod]
[Isolated]
public void VerifyStubWasCalled()
{
// Arrange
RealLogger fake = Isolate.Fake.Instance<RealLogger>();
Isolate.WhenCalled(() => fake.Increment()).IgnoreCall();

// Act            
fake.Increment();

// Assert
Isolate.Verify.WasCalledWithAnyArguments(() => fake.Increment());
}

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

  • PeteG

    I think AAA will make it easier for new agile developers to start using mocking frameworks, without having to get their heads around the artificial concepts of record and playback, which were really an implementation detail of the mock frameworks.

    My company is stuck with .Net 2.0 for the time being. Any chance of an AAA syntax for .Net 2.0, with no Lambdas ?

  • Gil Zilberfeld

    Hi PeteG,

    I believe that AAA are not just more readable, with our features, they also make write less code, and therefore create more robust tests.

    I’m sorry, but we don’t plan to make a 2.0 version of AAA at the moment.
    From a syntactic point of view, you can replace the Lambda with anonymous delegates:

    Isolate.WhenCalled(delegate {a.b();}).WillReturn(5);

    But since the AAA version is 3.5 compatible, you’ll need it anyway.

  • Anonymous

    I really like the new AAA concept – it’s compact, clear and clean. The problem for now is that some basic operations included in the “old” concept (such as “GetCallCount” and similar) are not yet available when using the AAA concept; that makes it very hard to write more thanbasic tests using AAA. You should really include those quickly in a new release!

  • Gil Zilberfeld

    It’s a work in process, but eventually you’ll get all the features we have today in the new syntax. Any specific thing you’re missing? (besides GetCallCount)

  • Bob

    Any chance you could make the code not run off to the side of the page?

TOP