The Assert Classes

The UnitTesting namespace also includes the Assert static type. This object contains methods for evaluating whether the results of a test were as expected. You call these static methods and expect a true/false condition. If the Boolean condition fails, the assertion fails. The assertions do not actually return results. Rather, they automatically notify the unit test framework at runtime if the assertion fails or succeeds.

As an example, you might write a unit test to load a known record from the database. You would then write assertions about this known record to prove that you can retrieve the data from the database and properly call the right sets and gets on a specific object. The following shows a simple assertion for testing that two variables contain the same value. If the values match (AreEqual), the assertion passes without issue. If the values don’t match, the assertion fails and the unit test framework marks the test as failed.

Assert.AreEqual(cust.Id, customerId);

The AreEqual method is just one example of the many assertion methods available to you from the Assert class. For the most part, these assertion methods are variations on a concept: compare two values and determine the results. Table 8.3 provides a more complete list.

Image

TABLE 8.3 Test Assertions

Each assertion method has a number of overloads. These overloads enable you to compare various data types, such as strings, numeric values, objects, and generic collections of objects, to one another. In addition, there are overloads that enable you to simply do the assertion and those that both do the assertion and enable you to enter a message that is displayed when the assertion fails.

The Assert class also contains a version of the AreEqual/AreNotEqual methods that use a generic data type. These methods enable you to compare two generic types against one another for equality. In this case, you indicate the generic type using standard generic notation, <T> (or (of T) in VB), and pass the two generic types you want to compare. The following shows an example.

Assert.AreEqual<Invoice>(cust1, cust2);

Verifying Collections of Objects (CollectionAssert)

The UnitTesting namespace also contains the assertion classes, CollectionAssert. With it, you can verify the contents of collection classes. For instance, you can call the Contains method to assert whether a given collection contains a specific element (or DoesNotContain). You can use AllItemsAreInstancesOfType to check that a collection only contains like instances. You can compare two collections to see if they are equal (AreEqual/AreNotEqual) or simply equivalent; they have the same elements but might be in a different order (AreEquivalent/AreNotEquivalent).

Verifying Strings (StringAssert)

The StringAssert class contains methods for verifying strings and portions of strings. For example, the Contains method enables you to check that a string contains a specific substring. You can use the StartsWith method to assert whether a string begins with a certain set of characters or use EndsWith to check the ending of a string. Finally, the Matches/DoesNotMatch methods enable you to check whether a string matches a regular expression you define.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset