Writing Human Readable Unit Tests in C# With FluentAssertions

Writing and maintaining unit tests is something I actively encourage all developers to keep on top of code quality and keeping bugs down. I’m also a believer that unit test suites should be self documenting whereby any developer should be able to dive into a code base and understand functionality based on the tests.

The main problem I find is that test can become cluttered with Asserts and test-specific code which becomes increasingly more difficult to read through. Recently I’ve come across FluentAssertions, a code library which helps transform tests using a syntax that reads naturally which has helped reduce the lines of code I’m writing while saving time reading tests.

FluentAssertions is a set of .NET extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style test. FluentAssertions has a huge range of extension methods that can you can leverage in your test suite, below are some examples of the extension methods I find myself using frequently.


Basic Assertions

            object player1 = null;
            object player2 = "Mario";


            player1 = player2;

           var title = "The Legend Of Zelda";

            //Example individual calls

            title.Should().Contain("Legend Of");
            title.Should().NotContain("Super Mario");            

            title.Should().HaveLength("The Legend Of Zelda".Length);

            title.Should().Be("The Legend Of Zelda");

            // chaining asserts together
            title.Should().StartWith("The").And.Contain("Legend Of").And.EndWith("Zelda");     
            bool hasCape = false;

            hasCape = true;
            int hp = 4;
            hp.Should().BeInRange(1, 10);

Not only does FluentAssertions provide a range of useful asserts for DateTime objects but it also provides a unique helper syntax for instantiating new DateTime objects which is far more readable at a glance than using the new DateTime syntax in your tests.

            var myBirthday = 1.March(1989).At(12, 00);




One of FluentAssertions best assets is its huge range of extension methods for asserting against collections, here are just a few examples of the main extension methods I find myself using on a daily basis however there are many more.

            IEnumerable drivers = new[] { "Mario", "Luigi", "Peach", "Yoshi" };           

            // Single Asserts
            drivers.Should().Equal(new List { "Mario", "Luigi", "Peach", "Yoshi" });            
            drivers.Should().BeEquivalentTo(new Collection { "Peach", "Yoshi", "Mario", "Luigi" });
            drivers.Should().NotBeEquivalentTo(new List { "Mario", "Luigi", "Peach", "Yoshi", "Bowser" });            
            drivers.Should().HaveSameCount(new[] { "Wario", "Bowser", "Donkey Kong", "Toad" });                     
            drivers.Should().ContainInOrder(new[] { "Mario", "Luigi", "Peach", "Yoshi" });


            var noDrivers = new string[0];
            drivers.Should().IntersectWith(new[] { "Mario", "Luigi", "Peach", "Yoshi", "Wario", "Bowser", "Donkey Kong", "Toad" });
            drivers.Should().NotIntersectWith(new List { "Waluigi", "Diddy Kong" });

            //chaining asserts together
                 .And.ContainInOrder(new[] { "Mario", "Luigi", "Peach", "Yoshi" })

Exception handling is also very clean using FluentAssertions with added flexibility of being able to assert against cases when exceptions shouldn’t be thrown (if absolutely necessary)

            // Assert exception is thrown
            Action equip = () => character.Equip(null));

             .And.Message.Contains("Can't Equip Null!");

            // Assert and check inner exception
            Action equip = () => character.Equip("Water Pistol");

                 .WithInnerMessage("Water Pistol is not a valid weapon");

            // Assert Exception is not thrown
            Action equip = () => character.equip("Master Sword"));

            // Assert specific type of exception isn't thrown 
            Action equip = () => character.equip("Big Goran Sord"));

These are just a few of the ways you can make use of FluentAssertions, you can find their full set of documentation here. I hope that you’re also able to incorporate FluentAssertions into your projects and that it makes you and your team more productive.

Web Development on a Chromebook Part 2: Setting Up Development Tools

Following on from part 1: Installing GalliumOS on a Chromebook – we should have full access to install applications and developer tools onto the Chromebook through GalliumOS (or any other Linux distribution). There are many tools and editors available for web development, in this tutorial we’re going to setup a complete web development environment by installing NodeJS – for running server side Javascript and installing packages, Git – for source control, Visual Studio Code my preferred code editor and a couple of NPM packages to generate some skeleton web applications to get us up and running quickly.

Installing NodeJS

NodeJs can be installed via the command line using the standard sudo apt-get install command however with GalliumOS, I’d strongly advise installing node via the Node Version Manager (NVM). I found this easier because when Node is installed using sudo, all node commands need to be executed with sudo permissions however this falls over when automated scripts try to run node/npm commands without sudo permissions and it becomes difficult to maintain. Using the NVM instead enables you to run any node/npm command without using sudo and it also enables multiple versions of node to be installed and managed on the same machine with he ability to switch versions on demand which is pretty cool.

To install the Node Version Manager run the following command:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

Once the NVM has been installed we can install the latest version of NodeJS

nvm install 5.10

One installed, run the following command to double check that you have a version of NodeJs installed:

nvm ls

Now node is installed, we just need to install Node’s package manager (NPM) and we’re ready to install VsCode

npm install -g


Installing Visual Studio Code

To install Visual Studio Code we first need to download it from and then extract it into a folder on our hard drive, I extracted it into the /opt/ folder on my filesystem however where you extract it doesn’t matter.

unzip VSCode-linux-x64.zip -d ~/path/to/VSCode

Once extracted, we can add the code command to our /usr/bin directory so that we can launch VsCode in any folder on our device using the code command.

sudo ln -s /path/to/VSCode/code /usr/local/bin/code

Now we’re all setup, navigate to a website directory on your filesystem or alternatively create an empty HTML file and run the following command to launch VsCode

code .

We’ve now turned our ChromeBook into a portable web development machine, stay tuned for more posts on how to be productive using visual studio code.



Web Development on a Chromebook Part 1: Installing GalliumOS

Since their launch in 2011, Chromebooks have been intriguing devices to myself and others who have been looking for a slim, light and portable machine with a decent battery life capable of running applications, code editors and source control on the go, the only problem being that they come bundled with ChromeOS. It’s well documented online that Chromebooks can run Ubuntu and other Linux distros which get the job done however at a cost of either taking up scarce hard drive space, potential hits to performance and/or reduced battery life which is why I held off for a while until I discovered the GalliumOS project.


GalliumOs is a fast, lightweight Linux distribution built on top of Xubuntu that has been specifically developed and optimized for ChromeOS devices. This offers a native Linux experience including optimized, working drivers for ChromeOS devices out of the box and it only takes up around 4GB space on the SSD (I was left with 11.5 GB on a 16GB SSD).

There are many Chromebooks available on the market however I chose to go with the Dell Chromebook 3120 (4GB version) as it has the best build quality according to reviews and you can get them with 4GB RAM which is now scarce with 11″ Chromebook models. I managed to pick one up for £169 + VAT on special offer from Dell in their sale.


Installing GalliumOS should be straight forward using the installation guide however some Chromebooks (Dell 3120 included) may require the removal of a “write-protection” screw on the motherboard before being able to modify the operating system on the device. In order to do this I have to open up my Chromebook by removing the screws on the back, opening the back cover, disconnecting the battery and removing the screw labelled “WP” which can be seen here. Once Write protection is enabled, the Chrombook needs to be put into “Developer Mode” and the firmware needs flashing using the johnlewis.ie scripts, there are full instructions for this procedure here for each processor type (the Dell 3120 uses the Bay Trail processor).

Following a successful firmware updated, the device is ready for GalliumOS to be installed, look here for instructions on how to create a bootable SD card/USB to install from. Those of you who are looking to dual boot from an external device, I’d strongly recommend using a 90mb/s SD Card like this one for the best experience however the SD card does stick out about an inch when in use.

With GalliumOS installed you’re now able to install any applications/packages compatible with Ubuntu which should include all of your favorite development tools. For those of you less experienced with Linux, in part 2 we’re going to look at getting a development environment up and running in GalliumOS with Node, Git and Visual Studio Code.