Custom assertion handlers increases the readability of your tests. Creating a new handler is easy and is done by adding a new extension method.

In the example below, we create a handler that verifies that a company has an address.

 
     public  class  Company 
     {
         public  string  Name { get ; set ; }
         public  Address  Address { get ; set ; }
     }
 
     public  class  Address 
     {
         public  string  StreetName { get ; set ; }
         public  string  PostalCode { get ; set ; }
         public  string  ZipCode { get ; set ; }
     }

The actual assertion handler is written as an extension method

 
     public  static  class  CustomAssertionHandler 
     {
         public  static  void  Address(this  Has <Company > verb)
         {
             VerbAssert <Company >.Assert(verb,c=>c.Address==null ,"Company has no address" );
         }
     }

The assertion handler is now available when writing unit test so that the tests are fluent in your own domain language. 

 
     [TestFixture ]
     public  class  CustomAssertionHandlerTest 
     {
         [Test ]
         public  void  TestCompanyForAddress()
         {
             var  company = new  Company ();
             Ensure .That(company).Has.Address();
         }
     }

Last edited May 3, 2011 at 6:39 PM by Arvander, version 6

Comments

No comments yet.