Fluent Assertions first look

I always loved Fluent Interfaces – when done properly they can make an API or library easier to use and understand. I’ve heard of Fluent Assertions before, but I confess I never gave it much attention. I usually use NUnit as my unit testing framework and I just though their API was good enough, and to be honest I didn’t want to waste much time learning yet another library. Just out of curiosity, I decided to take a look into their website today to take a look and try to understand what was the motivation behind Fluent Assertions (emphasis is mine):

Nothing is more annoying than a unit test that fails without clearly explaining why. More than often, you need to set a breakpoint and start up the debugger to be able to figure out what went wrong. (…) That’s why we designed Fluent Assertions to help you in this area. Not only by using clearly named assertion methods, but also by making sure the failure message provides as much information as possible.

I completely agree, sometimes you’re just wasting too much time trying to figure out what went wrong. I have to admit that some of the assertion messages provided by NUnit are not great, so I decided to run some tests and compare the messages between these two libraries.

Continue reading

Understanding LINQ method execution order

This is my answer to an interesting question asked yesterday on StackOverflow – what is the execution order of a LINQ query such as:

    var numbers = new[] { -1, 4, 9 };

    var sumOfRoots = numbers.Where(x => x > 0)
                            .Select(x => Math.Sqrt(x))
                            .Select(x => Math.Exp(x))
                            .Sum();

A quick an easy solution is to refactor the code in order to use custom delegates for each chained method (Where, Select and Sum), which makes things easier to debug. In this case I’m just printing a simple message to the console:

    static void Main(string[] args)
    {
        var numbers = new[] { -1, 4, 9 };

        double sum = numbers.Where(IsGreaterThanZero)
                            .Select(ToSquareRoot)       
                            .Select(ToExp)              
                            .Sum(x => ToNumber(x));

        Console.WriteLine($"{Environment.NewLine}Total = {sum}");

        Console.Read();
    }

    private static double ToNumber(double number)
    {
        Console.WriteLine($"ToNumber({number})");

        return number;
    }

    private static double ToSquareRoot(int number)
    {
        double value =  Math.Sqrt(number);

        Console.WriteLine($"Math.Sqrt({number}): {value}");

        return value;
    }

    private static double ToExp(double number)
    {
        double value =  Math.Exp(number);

        Console.WriteLine($"Math.Exp({number}): {value}");

        return value;
    }

    private static bool IsGreaterThanZero(int number)
    {
        bool isGreater = number > 0;

        Console.WriteLine($"{Environment.NewLine}{number} > 0: {isGreater}");

        return isGreater;
    }

The output is the following:
linq-order-output