Site Loader
Auckland, New Zealand
Fluent Assertions is a set of .Net extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style test. It runs on following frameworks
  • .Net 3.5,4.0 and 4.5
  • Windows store for Windows 8
  • Silverlight 4 and 5
  • Windows Phone 7.5 and 8
It also supports many unit test frameworks like
  • MSTEST
  • NUnit
  • XUnit
  • Gallio etc

Installation

You can download the Fluent Assertion from either as Nuget package or you can download the zip version Fluent Assertion from here To install Fluent Assertions using Nuget, just hit Fluent Assertions in the search term and you can see the latest version as shown below

Coding with Fluent Assertions

In this blog, I am just going to run some simple code which will illustrate how easily we can verify a complex stuff in code without doing much from our end.

String Verification

In this section we will see how we can verify strings using Fluent Assertion
Checking String of Empty
        public static void VerifyStringForEmpty(string actualString, string expectedString)
        {
            try
            {
                actualString.Should().NotBeEmpty("because we need to verify actual value");
                expectedString.Should().NotBeEmpty("because we need to verify actual value");
                Console.WriteLine("VerifyStringForEmpty is working fine !!!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
If you run the above code by calling this method, it will work fine (only if you pass non empty string for both actual and expected strings) If any of the strings are empty it will throw you an exception as shown below Calling the method
VerifyStringForEmpty("Karthik", "");
Output
error     As you could see above the message is pretty easy to understand by anyone which running the code and while getting error while asserting the piece of code.
Verifying String Formatting
        public static void VerifyStringForFormatting(string actualString)
        {
            try
            {
                actualString.Should().NotStartWith("$", "because we don't accept special charecters in string"); //Will pass
                actualString.Should().NotContainEquivalentOf("#$@!", "because we dont want special charecter even in middle of string"); //Will pass
                Console.WriteLine("VerifyStringForFormatting is working fine !!!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Verifying String Match
  public static void VerifyStringForMatch(string actualString, string expectedString)
        {
            try
            {
                actualString.ShouldBeEquivalentTo(expectedString, "because thats what is expected"); //will pass
                Console.WriteLine("VerifyStringForMatch is working fine !!!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Calling the above method
VerifyStringForMatch("Karthik", "Karthiks");
Output
error     As you could see above the expected was Karthiks, but we had actual as just Karthik, hence Fluent Assertion thrown as the exception message as shown.

Collections Verification

Here we are going to deal with some collection verification and see if I have the same count of value in my collection and has the value in the index as I am expecting as shown below
 public static void VerifyCollection()
        {
            try
            {
                IEnumerable collection = new[] { "karthik", "automation", "testing", "Mike" };
                collection.Should()
                    .NotBeEmpty().And.HaveCount(4)
                    .And.HaveElementAt(2, "testing", "because thats the value I am expecting in the collection");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

Dictionary

Let’s go even more deeper and see how Fluent Assertion will help us to check during while working with complex dictionaries For doing this I am going to create a custom type dictionary as shown below.
 Dictionary 
As you can see I have a type Employee, lets create that first
    public class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string EmployeeEmail { get; set; }
        public string EmployeeAddress { get; set; }
    }
Now lets add some value to the dictionary
 Dictionary empDict = new Dictionary
                {
                    { 1, new Employee() {EmployeeID = 100,Name="Karthik", EmployeeEmail="karthik@executeautomation.com", EmployeeAddress="Chennai,India"}},
                    {2, new Employee() { EmployeeID = 101,Name="Jack",EmployeeEmail="Jack@jill.com",EmployeeAddress="CA"} },
                    {3, new Employee() { EmployeeID=102,Name="Sam",EmployeeEmail="Sam@sung.com",EmployeeAddress="SFO"}},
                    {4, new Employee() { EmployeeID=103,Name="Max",EmployeeEmail="micro@max.com",EmployeeAddress="India" }}
                };
Now I would like to check if my
 empDict
has value with
 EmployeeID
102 Doing this in C# code programmatically needs a looping to check if the value exist or you can use lambda expressions. But using Fluent Assertion we can do it simply with the following code snippet
 empDict.Values.Should().Contain(a => a.EmployeeID == 102);
Putting it all together
    public static void VerifyDictionary()
        {
            try
            {
                DictionaryempDict = new Dictionary
                {
                    { 1, new Employee() {EmployeeID = 100,Name="Karthik", EmployeeEmail="karthik@executeautomation.com", EmployeeAddress="Chennai,India"}},
                    {2, new Employee() { EmployeeID = 101,Name="Jack",EmployeeEmail="Jack@jill.com",EmployeeAddress="CA"} },
                    {3, new Employee() { EmployeeID=102,Name="Sam",EmployeeEmail="Sam@sung.com",EmployeeAddress="SFO"}},
                    {4, new Employee() { EmployeeID=103,Name="Max",EmployeeEmail="micro@max.com",EmployeeAddress="India" }}
                };

                //Check if the Dictionary has Employee with EmployeeID 102
                empDict.Values.Should().Contain(a => a.EmployeeID == 102);

                Console.WriteLine("VerifyDictionary is working fine !!!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
The above code runs fine, since we have employeeId as 102 in dictionary, but lets say I am going to change it to 109 and run the same, I will get exception message from fluent assertion as shown below error3

HashTable

We can do the same with Hashtables as shown below
        public static void VerifyHashTables()
        {
            try
            {
                List empList = new List
            {
               new Employee() {EmployeeID = 100,Name="Karthik", EmployeeEmail="karthik@executeautomation.com", EmployeeAddress="Chennai,India"},
               new Employee() { EmployeeID = 101,Name="Jack",EmployeeEmail="Jack@jill.com",EmployeeAddress="CA"},
               new Employee() { EmployeeID=102,Name="Sam",EmployeeEmail="Sam@sung.com",EmployeeAddress="SFO"},
               new Employee() { EmployeeID=103,Name="Max",EmployeeEmail="micro@max.com",EmployeeAddress="India" }
            };


                empList.Should().HaveCount(4, "because thats the length of the list");
                empList.Should().Contain(a => a.Name == "Jack");

                Console.WriteLine("VerifyHashTables is working fine !!!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

Verifying Execution Time of Method

Using Fluent Assertion we can also check if a method takes longer time than expected. We can set the time limit for a method and see how much time the method is taking to execute. Let’s see how we can do the same. Here is my method which I am going to check how long its taking
        private void ExecuteSlowRunningProcess()
        {
            System.Threading.Thread.Sleep(3000);
        }
Here is how I can check using Fluent Assertions
        public static void CheckExecutionTimeLimit()
        {
            try
            {
                //Check if the method execution time exceed some value
                Program p = new Program();
                p.ExecutionTimeOf(v => v.ExecuteSlowRunningProcess()).ShouldNotExceed(2000.Milliseconds(), "because thats what the execution time of a method should be");

                Console.WriteLine("CheckExecutionTimeLimit is working fine !!!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
In the above code I have intentionally made to ShouldNotExceed method to 2000 millisecond and we know the called method is going to execute for 3000 millisecond. While I execute the above code I get the output as
Output
error4     As expected the test failed, since the we are expecting a method to complete is less than actually it does.

Summary

Fluent Assertions is really a very handy assertion mechanism to verify our code with TDD and BDD style. Its very easy to code and will come very handy while working in conjunction with MSTEST or NUnit frameworks. Please let your comments or questions if you have any. Thanks, Karthik KK

Post Author: Karthik kk

Leave a Reply

Your email address will not be published. Required fields are marked *