Book Image

Testing with F#

By : Mikael Lundin
Book Image

Testing with F#

By: Mikael Lundin

Overview of this book

Table of Contents (17 chapters)
Testing with F#
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Arrange, act, assert


To bring order into our unit tests, there are certain patterns to follow. One of the most common and popular patterns is the triple A (AAA) syntax, where the A's stand for:

  • Arrange

  • Act

  • Assert

The best way to visualize this is in code. Let's see how it works in C# code and then move on to the F# equivalent:

using NUnit.Framework;
using SUT = Company.System.BL.Calculators;

namespace Company.System.Tests.BL.Calculators.PriceCalculator
{
    public class GetPrice
    {
        [Test]
        public void ShouldReturnZeroOnEmptyOrder()
        {
            // Arrange
            var priceCalculator = new SUT.PriceCalculator();
            var order = new { };
            
            // Act
            var result = priceCalculator.GetPrice(order);

            // Assert
            Assert.That(result, Is.EqualTo(0), "Expecting zero price when order is empty");
        }
    }
}

I usually put the comments in there, to communicate to the reader that I'm using the AAA syntax, but...